Barnetik / SimpleForm

A simple way to working with forms in php.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SimpleForm

Build Status

A simple way to working with forms in php.

Install

composer require simettric/simple-form

Configure

$config = new Config();
$config->addFieldDefinition("customField", "\\Namespace\\CustomField");

Creating Forms

With the FormBuilder

$builder = new FormBuilder($config);

$builder->create("message")
        ->add("firstName")
        ->add("lastName")
        ->add("email", "email")
        ->add("subject", "choice", array("choices"=>array())) //InArray is implicit unless we configure our own ChoiceValidator in the "validators" key
        ->add("message", "textarea", array(
              "validators" => array(
                    new NotEmpty(), 
                    new StringLength(array("min"=>4))
        ));
        
$data_array = array("firstName"=>"John");
$form       = $builder->getForm($data_array);

Creating a Form class

class MessageForm extends AbstractForm{

    function configure(FormBuilder $builder){

        $this->name = "message";

        $builder->add("firstName")
                ->add("lastName")
                ->add("email", "email")
                ->add("subject", "choice", array("choices"=>array())) //ChoiceValidator is implicit unless we configure our own ChoiceValidator in the "validators" key
                ->add("message", "textarea", array(
                                              "validators" => array(
                                                    new NotEmpty(),
                                                    new StringLength(array("min"=>4))
                                            ));

    }


}

$data_array = array("firstName"=>"John");
$form       = new MessageForm($data_array, $config);

Validating Forms

SimpleForm uses Zend Validator to manage the fields validation in its forms.

$builder->add("message", "textarea", array(
              "label"      => "Write your message",
              "validators" => array(
                    new NotEmpty(), 
                    new StringLength(array("min"=>4)
              )
));

In your controller, you can bind the request data and check if the form is valid

$form->bind( $_POST["contact"] );

if($form->isValid()){

   echo $form->getValue("firstName");

}

Rendering Forms

<?php echo $form["firstName"] ?>

Outputs:

<div>
  
  <label for="contact_firstName">firstName</label>
  <input type="text" name="contact[firstName]" required="required">
  <span class="error">Error message</span>

</div>

You can render each HTML tag individually:

<?php echo $form["firstName"]->getLabelTag() ?>

<?php echo $form["firstName"]->getInputTag(array("class"=>"the attribute value")) ?>

<?php echo $form["firstName"]->getErrorTag() ?>

Also, you can get an array of error values

<?php foreach( $form["firstName"]->getErrorArray() as $error ){} ?>

About

A simple way to working with forms in php.

License:MIT License


Languages

Language:PHP 100.0%