2017 © Pedro PelĂĄez
 

library simple-form

A simple way to working with forms in php.

image

simettric/simple-form

A simple way to working with forms in php.

  • Thursday, December 22, 2016
  • by asiermarques
  • Repository
  • 2 Watchers
  • 9 Stars
  • 724 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 4 Versions
  • 6 % Grown

The README.md

SimpleForm

Build Status, (*1)

SensioLabsInsight, (*2)

A simple way to working with forms in php., (*3)

Install

composer require simettric/simple-form

Creating Forms

With the FormBuilder

$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);

Creating a Form class

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());

Validating Forms

SimpleForm uses Zend Validator to manage the fields validation in its forms., (*4)

$builder->add("message", new TextareaField(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, (*5)

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

if($form->isValid()){

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

}

Rendering Forms


Outputs:, (*6)



Error message

*Note: you can return a null value in your Form::getName() method in order to set a clean input name like:, (*7)

  <input type="text" name="firstName" required="required">

*Note2: using Field::getRow("label") method, the result is similar but with a custom label., (*8)

getRow('Text for the label tag') ?>

You can render each HTML tag individually:, (*9)

getLabelTag() ?>

getInputTag(array("class"=>"the attribute value")) ?>

getErrorTag() ?>

Also, you can get an array of error values, (*10)

getErrorArray() as $error ){} ?>

The Versions

22/12 2016

dev-master

9999999-dev

A simple way to working with forms in php.

  Sources   Download

MIT

The Requires

 

The Development Requires

library forms

07/12 2016

v1.1

1.1.0.0

A simple way to working with forms in php.

  Sources   Download

MIT

The Requires

 

The Development Requires

library forms

20/11 2016

v1.0

1.0.0.0

A simple way to working with forms in php.

  Sources   Download

MIT

The Requires

 

The Development Requires

library forms

09/11 2016

v0.1

0.1.0.0

A simple way to working with forms in php.

  Sources   Download

MIT

The Requires

 

The Development Requires

library forms