Skip to content
Snippets Groups Projects
Commit 8d02cf06 authored by Dyszczo's avatar Dyszczo
Browse files

Merge branch 'feature/form-action' into 'master'

feat(form): Action/method attribute for form

See merge request !6
parents 7631cb24 84800450
No related branches found
No related tags found
1 merge request!6feat(form): Action/method attribute for form
Pipeline #5089 failed
# Changelog
## [2.1.0] - 2020-07-17
### Added
- Action/method attribute for form
## [2.0.4] - 2020-06-24
### Added
- new button form field
......
......@@ -16,9 +16,7 @@ use WPDesk\Forms\Validator\RequiredValidator;
* @package WPDesk\Forms
*/
abstract class BasicField implements Field {
/** @var array[] */
protected $attributes;
use Field\Traits\HtmlAttributes;
/** @var array[] */
protected $meta;
......@@ -119,12 +117,6 @@ abstract class BasicField implements Field {
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 ) {
return $this->meta[ $name ];
}
......@@ -247,28 +239,6 @@ abstract class BasicField implements Field {
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 ) {
return isset( $this->meta[ $name ] );
}
......@@ -308,10 +278,6 @@ abstract class BasicField implements Field {
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() {
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;
use WPDesk\View\Renderer\Renderer;
class FormWithFields implements Form, ContainerForm, FieldProvider {
use Field\Traits\HtmlAttributes;
/**
* Unique form_id.
*
......@@ -44,6 +46,42 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
$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
*/
......@@ -156,8 +194,9 @@ class FormWithFields implements Form, ContainerForm, FieldProvider {
*/
public function render_form( Renderer $renderer ) {
$content = $renderer->render( 'form-start', [
'method' => 'POST',
'action' => '',
'form' => $this,
'method' => $this->get_method(), // backward compat
'action' => $this->get_action(), // backward compat
] );
$content .= $this->render_fields( $renderer );
$content .= $renderer->render( 'form-end' );
......
<?php
/**
* @var string $method
* @var string $action
* @var \WPDesk\Forms\Form\FormWithFields $form
*/
?>
<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 ?>
<table class="form-table">
......
......@@ -2,7 +2,7 @@
namespace Tests;
use WPDesk\Forms\AbstractForm;
use WPDesk\Forms\Form\AbstractForm;
class TestForm extends \PHPUnit\Framework\TestCase
{
......
......@@ -2,8 +2,8 @@
namespace Tests;
use WPDesk\Forms\AbstractForm;
use WPDesk\Forms\FormsCollection;
use WPDesk\Forms\Form\AbstractForm;
use WPDesk\Forms\Form\FormsCollection;
class TestFormCollection extends \PHPUnit\Framework\TestCase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment