Skip to content
Snippets Groups Projects
Commit 41941369 authored by potreb's avatar potreb
Browse files

fix: html attributes, templates fix

parent a32c016e
No related branches found
No related tags found
3 merge requests!28release: 3.0.0,!23Feature/strong typing pp,!19Add strong typing for 3.0 version
Pipeline #6487 passed
......@@ -156,33 +156,32 @@ abstract class BasicField implements Field {
return $this->attributes['id'] ?? sanitize_title( $this->get_name() );
}
final public function is_multiple(): bool {
return $this->attributes['multiple'];
return isset( $this->attributes['multiple'] );
}
final public function set_disabled(): Field {
$this->attributes['disabled'] = true;
$this->attributes['disabled'] = 'disabled';
return $this;
}
final public function is_disabled(): bool {
return $this->attributes['disabled'];
return $this->attributes['disabled'] ?? false;
}
final public function set_readonly(): Field {
$this->attributes['readonly'] = true;
$this->attributes['readonly'] = 'readonly';
return $this;
}
final public function is_readonly(): bool {
return $this->attributes['readonly'];
return $this->attributes['readonly'] ?? false;
}
final public function set_required(): Field {
$this->attributes['required'] = true;
$this->attributes['required'] = 'required';
return $this;
}
......@@ -233,7 +232,7 @@ abstract class BasicField implements Field {
}
final public function is_required(): bool {
return $this->attributes['required'];
return isset( $this->attributes['required'] );
}
final public function get_priority(): int {
......
......@@ -18,7 +18,7 @@ class SelectField extends BasicField {
}
public function set_multiple(): Field {
$this->attributes['multiple'] = true;
$this->attributes['multiple'] = 'multiple';
return $this;
}
......
......@@ -12,19 +12,8 @@ use WPDesk\Forms\Form;
*/
trait HtmlAttributes {
/** @var array{placeholder: string, name: string, id: string, class: string[], readonly: bool, multiple: bool, disabled: bool, required: bool, method: string, action: string} */
protected $attributes = [
'placeholder' => '',
'name' => '',
'id' => '',
'class' => [],
'action' => '',
'method' => 'POST',
'readonly' => false,
'multiple' => false,
'disabled' => false,
'required' => false,
];
/** @var array{placeholder: string, name: string, id: string, class: string[]} */
protected $attributes = [];
/**
* Get list of all attributes except given.
......@@ -33,13 +22,13 @@ trait HtmlAttributes {
*
* @return array<string[]|string|bool>
*/
final public function get_attributes( array $except = [ 'name', 'class', 'method', 'action' ] ): array {
final public function get_attributes( array $except = [ 'name', 'class' ] ): array {
return array_filter(
$this->attributes,
static function ( $attribute, $key ) use ( $except ) {
return ! in_array( $key, $except, true ) && ! empty( $attribute );
static function ( $key ) use ( $except ) {
return ! in_array( $key, $except, true );
},
ARRAY_FILTER_USE_BOTH
ARRAY_FILTER_USE_KEY
);
}
......@@ -73,6 +62,7 @@ trait HtmlAttributes {
// Be aware of coercing - if implode returns string(0) '', then return $default value.
return implode( ' ', $this->attributes[ $name ] ) ?: $default ?? '';
}
return (string) $this->attributes[ $name ] ?? $default ?? '';
}
}
......@@ -32,6 +32,8 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
public function __construct( array $fields, string $form_id = 'form' ) {
$this->fields = $fields;
$this->form_id = $form_id;
$this->set_action( '' );
$this->set_method( 'POST' );
}
/** Set Form action attribute. */
......@@ -41,6 +43,10 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this;
}
public function get_action(): string {
return $this->attributes['action'];
}
/** Set Form method attribute ie. GET/POST. */
public function set_method( string $method ): self {
$this->attributes['method'] = $method;
......@@ -52,9 +58,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this->attributes['method'];
}
public function get_action(): string {
return $this->attributes['action'];
}
public function is_submitted(): bool {
return null !== $this->updated_data;
......
......@@ -21,20 +21,15 @@ class JsonNormalizedRenderer implements FieldRenderer {
public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array {
$rendered_fields = [];
foreach ( $provider->get_fields() as $field ) {
$rendered = [
'name' => $field->get_name(),
'template' => $field->get_template_name(),
'multiple' => $field->is_multiple(),
'disabled' => $field->is_disabled(),
'readonly' => $field->is_readonly(),
'required' => $field->is_required(),
'prefix' => $name_prefix,
'value ' => $fields_data[ $field->get_name() ] ?? $field->get_default_value(),
];
$rendered = [];
foreach ( $field->get_attributes() as $key => $attribute ) {
$rendered[ $key ] = $attribute;
}
$rendered['name'] = $field->get_name();
$rendered['template'] = $field->get_template_name();
$rendered['prefix'] = $name_prefix;
$rendered['value'] = $fields_data[ $field->get_name() ] ?? $field->get_default_value();
if ( $field->has_classes() ) {
$rendered['class'] = $field->get_classes();
}
if ( $field->has_description() ) {
$rendered['description'] = $field->get_description();
}
......@@ -44,9 +39,6 @@ class JsonNormalizedRenderer implements FieldRenderer {
if ( $field->has_label() ) {
$rendered['label'] = $field->get_label();
}
if ( $field->has_placeholder() ) {
$rendered['placeholder'] = $field->get_placeholder();
}
$options = $field->get_possible_values();
if ( $options ) {
$rendered['options'] = $options;
......
......@@ -10,27 +10,16 @@
?>
<button
type="<?php echo \esc_attr( $field->get_type() ); ?>"
name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>"
id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $value ); ?>"
<?php if ( $field->has_classes() ) : ?>
class="<?php echo \esc_attr( $field->get_classes() ); ?>"
<?php endif; ?>
<?php foreach ( $field->get_attributes( [] ) as $key => $val ) : ?>
<?php foreach ( $field->get_attributes() as $key => $val ) : ?>
<?php echo \esc_attr( $key ) . '="' . \esc_attr( $val ) . '"'; ?>
<?php endforeach; ?>
type="<?php echo \esc_attr( $field->get_type() ); ?>"
name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>"
id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $value ); ?>"
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
><?php echo \esc_html( $field->get_label() ); ?></button>
......@@ -24,18 +24,6 @@
name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( $field->get_name() ); ?>]"
id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $field->get_label() ); ?>"
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
/>
</p>
</td>
......
......@@ -43,18 +43,6 @@ if ( empty( $value ) || is_string( $value ) ) {
?>
<?php endforeach; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?>
value="<?php echo \esc_html( $text_value ); ?>"
<?php else : ?>
......
......@@ -23,28 +23,12 @@ if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) :
class="<?php echo \esc_attr( $field->get_classes() ); ?>"
<?php endif; ?>
<?php if ( $field->get_type() === 'text' && $field->has_placeholder() ) : ?>
placeholder="<?php echo \esc_html( $field->get_placeholder() ); ?>"
<?php endif; ?>
<?php
foreach ( $field->get_attributes() as $key => $atr_val ) :
echo \esc_attr( $key ) . '="' . \esc_attr( $atr_val ) . '"';
?>
<?php endforeach; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?>
value="<?php echo \esc_html( $value ); ?>"
<?php else : ?>
......
......@@ -17,23 +17,6 @@
<?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?>
<?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>"
<?php endforeach; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_multiple() ) :
?>
multiple="multiple"<?php endif; ?>
>
<?php
if ( $field->has_placeholder() ) :
......
......@@ -18,23 +18,6 @@
<?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>"
<?php endforeach; ?>
<?php
if ( $field->is_required() ) :
?>
required="required"<?php endif; ?>
<?php
if ( $field->is_disabled() ) :
?>
disabled="disabled"<?php endif; ?>
<?php
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
<?php
if ( $field->is_multiple() ) :
?>
multiple="multiple"<?php endif; ?>
<?php
if ( $field->has_placeholder() ) :
?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment