Skip to content
Snippets Groups Projects
Commit e2742f21 authored by dyszczo's avatar dyszczo
Browse files

feat(form): Action/method attribute for form

parent 7631cb24
No related branches found
No related tags found
1 merge request!6feat(form): Action/method attribute for form
Pipeline #5031 failed
# Changelog # Changelog
## [2.1.0] - 2020-07-17
### Added
- Action/method attribute for form
## [2.0.4] - 2020-06-24 ## [2.0.4] - 2020-06-24
### Added ### Added
- new button form field - new button form field
......
...@@ -16,9 +16,7 @@ use WPDesk\Forms\Validator\RequiredValidator; ...@@ -16,9 +16,7 @@ use WPDesk\Forms\Validator\RequiredValidator;
* @package WPDesk\Forms * @package WPDesk\Forms
*/ */
abstract class BasicField implements Field { abstract class BasicField implements Field {
use Field\Traits\HtmlAttributes;
/** @var array[] */
protected $attributes;
/** @var array[] */ /** @var array[] */
protected $meta; protected $meta;
...@@ -119,12 +117,6 @@ abstract class BasicField implements Field { ...@@ -119,12 +117,6 @@ abstract class BasicField implements Field {
return $this; return $this;
} }
public function get_attributes( $except = [] ) {
return array_filter( $this->attributes, function ( $value, $key ) use ( $except ) {
return ! in_array( $key, $except );
}, ARRAY_FILTER_USE_BOTH );
}
public function get_meta_value( $name ) { public function get_meta_value( $name ) {
return $this->meta[ $name ]; return $this->meta[ $name ];
} }
...@@ -247,28 +239,6 @@ abstract class BasicField implements Field { ...@@ -247,28 +239,6 @@ abstract class BasicField implements Field {
return $this; return $this;
} }
/**
* @param $name
* @param $value
*
* @return $this
*/
public function set_attribute( $name, $value ) {
$this->attributes[ $name ] = $value;
return $this;
}
public function unset_attribute( $name ) {
unset( $this->attributes[ $name ] );
return $this;
}
public function is_attribute_set( $name ) {
return isset( $this->attributes[ $name ] );
}
public function is_meta_value_set( $name ) { public function is_meta_value_set( $name ) {
return isset( $this->meta[ $name ] ); return isset( $this->meta[ $name ] );
} }
...@@ -308,10 +278,6 @@ abstract class BasicField implements Field { ...@@ -308,10 +278,6 @@ abstract class BasicField implements Field {
return isset( $this->meta['required'] ) ? $this->meta['required'] : false; return isset( $this->meta['required'] ) ? $this->meta['required'] : false;
} }
public function get_attribute( $name, $default = null ) {
return isset( $this->attributes[ $name ] ) ?: $default;
}
public function get_sanitizer() { public function get_sanitizer() {
return new NoSanitize(); return new NoSanitize();
} }
......
<?php
namespace WPDesk\Forms\Field\Traits;
/**
* Implementation of HTML attributes like id, name, action etc.
*
* @package WPDesk\Forms\Field\Traits
*/
trait HtmlAttributes {
/** @var string[] */
protected $attributes;
/**
* Get list of all attributes except given.
*
* @param string[] $except
*
* @return string[]
*/
public function get_attributes( $except = [] ) {
return array_filter( $this->attributes, function ( $value, $key ) use ( $except ) {
return ! in_array( $key, $except, true );
}, ARRAY_FILTER_USE_BOTH );
}
/**
* @param string $name
* @param string $value
*
* @return $this
*/
public function set_attribute( $name, $value ) {
$this->attributes[ $name ] = $value;
return $this;
}
/**
* @param string $name
*
* @return $this
*/
public function unset_attribute( $name ) {
unset( $this->attributes[ $name ] );
return $this;
}
/**
* @param string $name
*
* @return bool
*/
public function is_attribute_set( $name ) {
return isset( $this->attributes[ $name ] );
}
/**
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function get_attribute( $name, $default = null ) {
return isset( $this->attributes[ $name ] ) ?: $default;
}
}
\ No newline at end of file
...@@ -13,6 +13,8 @@ use WPDesk\Persistence\PersistentContainer; ...@@ -13,6 +13,8 @@ use WPDesk\Persistence\PersistentContainer;
use WPDesk\View\Renderer\Renderer; use WPDesk\View\Renderer\Renderer;
class FormWithFields implements Form, ContainerForm, FieldProvider { class FormWithFields implements Form, ContainerForm, FieldProvider {
use Field\Traits\HtmlAttributes;
/** /**
* Unique form_id. * Unique form_id.
* *
...@@ -44,6 +46,42 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -44,6 +46,42 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
$this->updated_data = null; $this->updated_data = null;
} }
/**
* Set Form action attribute.
*
* @param string $action
*/
public function set_action( $action ) {
$this->attributes['action'] = $action;
return $this;
}
/**
* Set Form method attribute ie. GET/POST.
*
* @param string $method
*/
public function set_method( $method ) {
$this->attributes['method'] = $method;
return $this;
}
/**
* @return string
*/
public function get_method() {
return isset( $this->attributes['method'] ) ? $this->attributes['method'] : 'POST';
}
/**
* @return string
*/
public function get_action() {
return isset( $this->attributes['action'] ) ? $this->attributes['action'] : '';
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
...@@ -156,8 +194,9 @@ class FormWithFields implements Form, ContainerForm, FieldProvider { ...@@ -156,8 +194,9 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
*/ */
public function render_form( Renderer $renderer ) { public function render_form( Renderer $renderer ) {
$content = $renderer->render( 'form-start', [ $content = $renderer->render( 'form-start', [
'method' => 'POST', 'form' => $this,
'action' => '', 'method' => $this->get_method(), // backward compat
'action' => $this->get_action(), // backward compat
] ); ] );
$content .= $this->render_fields( $renderer ); $content .= $this->render_fields( $renderer );
$content .= $renderer->render( 'form-end' ); $content .= $renderer->render( 'form-end' );
......
<?php <?php
/** /**
* @var string $method * @var \WPDesk\Forms\Form\FormWithFields $form
* @var string $action
*/ */
?> ?>
<form class="wrap woocommerce" method="<?php echo esc_attr($method); ?>" action="<?php echo esc_attr($action); ?>"> <form class="wrap woocommerce" method="<?php echo esc_attr($form->get_method()); ?>" action="<?php echo esc_attr($form->get_action()); ?>">
<h2 style="display:none;"></h2><?php // All admin notices will be moved here by WP js ?> <h2 style="display:none;"></h2><?php // All admin notices will be moved here by WP js ?>
<table class="form-table"> <table class="form-table">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment