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 with stages
in 1 minute and 17 seconds
...@@ -25,12 +25,12 @@ abstract class BasicField implements Field { ...@@ -25,12 +25,12 @@ abstract class BasicField implements Field {
/** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */ /** @var array{default_value: string, possible_values?: string[], sublabel?: string, priority: int, label: string, description: string, description_tip: string, data: array<string|int>} */
protected $meta = [ protected $meta = [
'priority' => self::DEFAULT_PRIORITY, 'priority' => self::DEFAULT_PRIORITY,
'default_value' => '', 'default_value' => '',
'label' => '', 'label' => '',
'description' => '', 'description' => '',
'description_tip' => '', 'description_tip' => '',
'data' => [], 'data' => [],
]; ];
public function should_override_form_template(): bool { public function should_override_form_template(): bool {
...@@ -59,7 +59,7 @@ abstract class BasicField implements Field { ...@@ -59,7 +59,7 @@ abstract class BasicField implements Field {
} }
public function get_serializer(): Serializer { public function get_serializer(): Serializer {
throw new BadMethodCallException('You must define your serializer in a child class.'); throw new BadMethodCallException( 'You must define your serializer in a child class.' );
} }
final public function get_name(): string { final public function get_name(): string {
...@@ -156,33 +156,32 @@ abstract class BasicField implements Field { ...@@ -156,33 +156,32 @@ abstract class BasicField implements Field {
return $this->attributes['id'] ?? sanitize_title( $this->get_name() ); return $this->attributes['id'] ?? sanitize_title( $this->get_name() );
} }
final public function is_multiple(): bool { final public function is_multiple(): bool {
return $this->attributes['multiple']; return isset( $this->attributes['multiple'] );
} }
final public function set_disabled(): Field { final public function set_disabled(): Field {
$this->attributes['disabled'] = true; $this->attributes['disabled'] = 'disabled';
return $this; return $this;
} }
final public function is_disabled(): bool { final public function is_disabled(): bool {
return $this->attributes['disabled']; return $this->attributes['disabled'] ?? false;
} }
final public function set_readonly(): Field { final public function set_readonly(): Field {
$this->attributes['readonly'] = true; $this->attributes['readonly'] = 'readonly';
return $this; return $this;
} }
final public function is_readonly(): bool { final public function is_readonly(): bool {
return $this->attributes['readonly']; return $this->attributes['readonly'] ?? false;
} }
final public function set_required(): Field { final public function set_required(): Field {
$this->attributes['required'] = true; $this->attributes['required'] = 'required';
return $this; return $this;
} }
...@@ -233,7 +232,7 @@ abstract class BasicField implements Field { ...@@ -233,7 +232,7 @@ abstract class BasicField implements Field {
} }
final public function is_required(): bool { final public function is_required(): bool {
return $this->attributes['required']; return isset( $this->attributes['required'] );
} }
final public function get_priority(): int { final public function get_priority(): int {
......
...@@ -18,7 +18,7 @@ class SelectField extends BasicField { ...@@ -18,7 +18,7 @@ class SelectField extends BasicField {
} }
public function set_multiple(): Field { public function set_multiple(): Field {
$this->attributes['multiple'] = true; $this->attributes['multiple'] = 'multiple';
return $this; return $this;
} }
......
...@@ -12,19 +12,8 @@ use WPDesk\Forms\Form; ...@@ -12,19 +12,8 @@ use WPDesk\Forms\Form;
*/ */
trait HtmlAttributes { trait HtmlAttributes {
/** @var array{placeholder: string, name: string, id: string, class: string[], readonly: bool, multiple: bool, disabled: bool, required: bool, method: string, action: string} */ /** @var array{placeholder: string, name: string, id: string, class: string[]} */
protected $attributes = [ protected $attributes = [];
'placeholder' => '',
'name' => '',
'id' => '',
'class' => [],
'action' => '',
'method' => 'POST',
'readonly' => false,
'multiple' => false,
'disabled' => false,
'required' => false,
];
/** /**
* Get list of all attributes except given. * Get list of all attributes except given.
...@@ -33,18 +22,18 @@ trait HtmlAttributes { ...@@ -33,18 +22,18 @@ trait HtmlAttributes {
* *
* @return array<string[]|string|bool> * @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( return array_filter(
$this->attributes, $this->attributes,
static function ( $attribute, $key ) use ( $except ) { static function ( $key ) use ( $except ) {
return ! in_array( $key, $except, true ) && ! empty( $attribute ); return ! in_array( $key, $except, true );
}, },
ARRAY_FILTER_USE_BOTH ARRAY_FILTER_USE_KEY
); );
} }
/** /**
* @param string $name * @param string $name
* @param string[]|string|bool $value * @param string[]|string|bool $value
* *
* @return Field|Form * @return Field|Form
...@@ -73,6 +62,7 @@ trait HtmlAttributes { ...@@ -73,6 +62,7 @@ trait HtmlAttributes {
// Be aware of coercing - if implode returns string(0) '', then return $default value. // Be aware of coercing - if implode returns string(0) '', then return $default value.
return implode( ' ', $this->attributes[ $name ] ) ?: $default ?? ''; return implode( ' ', $this->attributes[ $name ] ) ?: $default ?? '';
} }
return (string) $this->attributes[ $name ] ?? $default ?? ''; return (string) $this->attributes[ $name ] ?? $default ?? '';
} }
} }
...@@ -26,12 +26,14 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -26,12 +26,14 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
/** /**
* FormWithFields constructor. * FormWithFields constructor.
* *
* @param Field[] $fields * @param Field[] $fields
* @param string $form_id * @param string $form_id
*/ */
public function __construct( array $fields, string $form_id = 'form' ) { public function __construct( array $fields, string $form_id = 'form' ) {
$this->fields = $fields; $this->fields = $fields;
$this->form_id = $form_id; $this->form_id = $form_id;
$this->set_action( '' );
$this->set_method( 'POST' );
} }
/** Set Form action attribute. */ /** Set Form action attribute. */
...@@ -41,6 +43,10 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -41,6 +43,10 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this; return $this;
} }
public function get_action(): string {
return $this->attributes['action'];
}
/** Set Form method attribute ie. GET/POST. */ /** Set Form method attribute ie. GET/POST. */
public function set_method( string $method ): self { public function set_method( string $method ): self {
$this->attributes['method'] = $method; $this->attributes['method'] = $method;
...@@ -52,9 +58,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -52,9 +58,6 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
return $this->attributes['method']; return $this->attributes['method'];
} }
public function get_action(): string {
return $this->attributes['action'];
}
public function is_submitted(): bool { public function is_submitted(): bool {
return null !== $this->updated_data; return null !== $this->updated_data;
...@@ -146,7 +149,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -146,7 +149,7 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
} }
public function render_form( Renderer $renderer ): string { public function render_form( Renderer $renderer ): string {
$content = $renderer->render( $content = $renderer->render(
'form-start', 'form-start',
[ [
'form' => $this, 'form' => $this,
......
...@@ -13,28 +13,23 @@ use WPDesk\Forms\FieldRenderer; ...@@ -13,28 +13,23 @@ use WPDesk\Forms\FieldRenderer;
class JsonNormalizedRenderer implements FieldRenderer { class JsonNormalizedRenderer implements FieldRenderer {
/** /**
* @param FieldProvider $provider * @param FieldProvider $provider
* @param array $fields_data * @param array $fields_data
* @param string $name_prefix * @param string $name_prefix
* *
* @return array Normalized fields with data. * @return array Normalized fields with data.
*/ */
public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array { public function render_fields( FieldProvider $provider, array $fields_data, string $name_prefix = '' ): array {
$rendered_fields = []; $rendered_fields = [];
foreach ( $provider->get_fields() as $field ) { foreach ( $provider->get_fields() as $field ) {
$rendered = [ $rendered = [];
'name' => $field->get_name(), foreach ( $field->get_attributes() as $key => $attribute ) {
'template' => $field->get_template_name(), $rendered[ $key ] = $attribute;
'multiple' => $field->is_multiple(), }
'disabled' => $field->is_disabled(), $rendered['name'] = $field->get_name();
'readonly' => $field->is_readonly(), $rendered['template'] = $field->get_template_name();
'required' => $field->is_required(), $rendered['prefix'] = $name_prefix;
'prefix' => $name_prefix, $rendered['value'] = $fields_data[ $field->get_name() ] ?? $field->get_default_value();
'value ' => $fields_data[ $field->get_name() ] ?? $field->get_default_value(),
];
if ( $field->has_classes() ) {
$rendered['class'] = $field->get_classes();
}
if ( $field->has_description() ) { if ( $field->has_description() ) {
$rendered['description'] = $field->get_description(); $rendered['description'] = $field->get_description();
} }
...@@ -44,9 +39,6 @@ class JsonNormalizedRenderer implements FieldRenderer { ...@@ -44,9 +39,6 @@ class JsonNormalizedRenderer implements FieldRenderer {
if ( $field->has_label() ) { if ( $field->has_label() ) {
$rendered['label'] = $field->get_label(); $rendered['label'] = $field->get_label();
} }
if ( $field->has_placeholder() ) {
$rendered['placeholder'] = $field->get_placeholder();
}
$options = $field->get_possible_values(); $options = $field->get_possible_values();
if ( $options ) { if ( $options ) {
$rendered['options'] = $options; $rendered['options'] = $options;
......
...@@ -10,27 +10,16 @@ ...@@ -10,27 +10,16 @@
?> ?>
<button <button
<?php if ( $field->has_classes() ) : ?>
class="<?php echo \esc_attr( $field->get_classes() ); ?>"
<?php endif; ?>
<?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() ); ?>" type="<?php echo \esc_attr( $field->get_type() ); ?>"
name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>" name="<?php echo \esc_attr( $name_prefix ) . '[' . \esc_attr( $field->get_name() ) . ']'; ?>"
id="<?php echo \esc_attr( $field->get_id() ); ?>" id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $value ); ?>" value="<?php echo \esc_html( $value ); ?>"
<?php if ( $field->has_classes() ) : ?>
<?php class="<?php echo \esc_attr( $field->get_classes() ); ?>"
if ( $field->is_disabled() ) : <?php endif; ?>
?> <?php foreach ( $field->get_attributes() as $key => $val ) : ?>
disabled="disabled"<?php endif; ?> <?php echo \esc_attr( $key ) . '="' . \esc_attr( $val ) . '"'; ?>
<?php <?php endforeach; ?>
if ( $field->is_readonly() ) :
?>
readonly="readonly"<?php endif; ?>
><?php echo \esc_html( $field->get_label() ); ?></button> ><?php echo \esc_html( $field->get_label() ); ?></button>
...@@ -24,18 +24,6 @@ ...@@ -24,18 +24,6 @@
name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( $field->get_name() ); ?>]" name="<?php echo \esc_attr( $name_prefix ); ?>[<?php echo \esc_attr( $field->get_name() ); ?>]"
id="<?php echo \esc_attr( $field->get_id() ); ?>" id="<?php echo \esc_attr( $field->get_id() ); ?>"
value="<?php echo \esc_html( $field->get_label() ); ?>" 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> </p>
</td> </td>
......
...@@ -43,18 +43,6 @@ if ( empty( $value ) || is_string( $value ) ) { ...@@ -43,18 +43,6 @@ if ( empty( $value ) || is_string( $value ) ) {
?> ?>
<?php endforeach; ?> <?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 ) ) : ?> <?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?>
value="<?php echo \esc_html( $text_value ); ?>" value="<?php echo \esc_html( $text_value ); ?>"
<?php else : ?> <?php else : ?>
......
...@@ -23,28 +23,12 @@ if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) : ...@@ -23,28 +23,12 @@ if ( $field->get_type() === 'checkbox' && $field->has_sublabel() ) :
class="<?php echo \esc_attr( $field->get_classes() ); ?>" class="<?php echo \esc_attr( $field->get_classes() ); ?>"
<?php endif; ?> <?php endif; ?>
<?php if ( $field->get_type() === 'text' && $field->has_placeholder() ) : ?>
placeholder="<?php echo \esc_html( $field->get_placeholder() ); ?>"
<?php endif; ?>
<?php <?php
foreach ( $field->get_attributes() as $key => $atr_val ) : foreach ( $field->get_attributes() as $key => $atr_val ) :
echo \esc_attr( $key ) . '="' . \esc_attr( $atr_val ) . '"'; echo \esc_attr( $key ) . '="' . \esc_attr( $atr_val ) . '"';
?> ?>
<?php endforeach; ?> <?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 ) ) : ?> <?php if ( \in_array( $field->get_type(), [ 'number', 'text', 'hidden' ], true ) ) : ?>
value="<?php echo \esc_html( $value ); ?>" value="<?php echo \esc_html( $value ); ?>"
<?php else : ?> <?php else : ?>
......
...@@ -17,23 +17,6 @@ ...@@ -17,23 +17,6 @@
<?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?> <?php foreach ( $field->get_attributes() as $key => $attr_val ) : ?>
<?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>" <?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>"
<?php endforeach; ?> <?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 <?php
if ( $field->has_placeholder() ) : if ( $field->has_placeholder() ) :
......
...@@ -18,23 +18,6 @@ ...@@ -18,23 +18,6 @@
<?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>" <?php echo \esc_attr( $key ); ?>="<?php echo \esc_attr( $attr_val ); ?>"
<?php endforeach; ?> <?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 <?php
if ( $field->has_placeholder() ) : if ( $field->has_placeholder() ) :
?> ?>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment