Skip to content
Snippets Groups Projects
Select Git revision
  • 4194136993983cdb5504534df5637cba2d53ecaa
  • master default protected
  • devel
  • feature/add-escaping-to-templates
  • feature/add-priority-sorting
  • 3.3.0
  • 3.2.1
  • 3.2.0
  • 3.1.0
  • 3.0.0
  • 2.4.12
  • 2.4.11
  • 2.4.10
  • 2.4.9
  • 2.4.8
  • 2.4.7
  • 2.4.6
  • 2.4.5
  • 2.4.4
  • 2.4.2
  • 2.4.1
  • 2.4.0
  • 2.3.2
  • 2.3.1
  • 2.3
25 results

FormWithFields.php

Blame
  • FormWithFields.php 4.96 KiB
    <?php
    
    namespace WPDesk\Forms\Form;
    
    use Psr\Container\ContainerInterface;
    use WPDesk\Forms\ContainerForm;
    use WPDesk\Forms\Field;
    use WPDesk\Forms\FieldProvider;
    use WPDesk\Forms\Form;
    use WPDesk\Persistence\ElementNotExistsException;
    use WPDesk\Persistence\PersistentContainer;
    use WPDesk\View\Renderer\Renderer;
    
    class FormWithFields implements Form, ContainerForm, FieldProvider {
    	use Field\Traits\HtmlAttributes;
    
    	/** @var string Unique form_id. */
    	protected $form_id = 'form';
    
    	/** @var array Updated data. */
    	private $updated_data;
    
    	/** @var Field[] Form fields. */
    	private $fields;
    
    	/**
    	 * FormWithFields constructor.
    	 *
    	 * @param Field[] $fields
    	 * @param string  $form_id
    	 */
    	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. */
    	public function set_action( string $action ): self {
    		$this->attributes['action'] = $action;
    
    		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;
    
    		return $this;
    	}
    
    	public function get_method(): string {
    		return $this->attributes['method'];
    	}
    
    
    	public function is_submitted(): bool {
    		return null !== $this->updated_data;
    	}
    
    	/** @return void */
    	public function add_field( Field $field ) {
    		$this->fields[] = $field;
    	}