PHP code example of simettric / simple-form

1. Go to this page and download the library: Download simettric/simple-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simettric / simple-form example snippets


$builder = new FormBuilder($config);

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

class MessageForm extends AbstractForm{

    public function configure(FormBuilder $builder)
    {


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

    }
    
    
    public function getName()
    {
        return 'message';
    }


}

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

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

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

if($form->isValid()){

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

}

 echo $form["firstName"] 

 echo $form["firstName"]->getRow('Text for the label tag') 
    
 echo $form["firstName"]->getLabelTag() 
     
 foreach( $form["firstName"]->getErrorArray() as $error ){}