PHPackages                             simettric/simple-form - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. simettric/simple-form

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

simettric/simple-form
=====================

A simple way to working with forms in php.

v1.1(9y ago)91.9k3MITPHPPHP &gt;=5.4

Since Nov 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/Simettric/SimpleForm)[ Packagist](https://packagist.org/packages/simettric/simple-form)[ RSS](/packages/simettric-simple-form/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

SimpleForm
==========

[](#simpleform)

[![Build Status](https://camo.githubusercontent.com/cb832e1d9f0b5bd9482b7e3e8652f3476ce57f8890719bed13a2b51d94a0453e/68747470733a2f2f7472617669732d63692e6f72672f53696d6574747269632f53696d706c65466f726d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Simettric/SimpleForm)

[![SensioLabsInsight](https://camo.githubusercontent.com/4e6c2814974516f5fe0c20e5a0bb417757c70d86d641d4fdb23dcf69c111f861/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f62393564653731622d663137382d346465302d613434622d3463353565623236323539392f6269672e706e67)](https://insight.sensiolabs.com/projects/b95de71b-f178-4de0-a44b-4c55eb262599)

A simple way to working with forms in php.

### Install

[](#install)

```
composer require simettric/simple-form

```

### Creating Forms

[](#creating-forms)

#### With the FormBuilder

[](#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

[](#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

[](#validating-forms)

SimpleForm uses [Zend Validator](http://framework.zend.com/manual/current/en/modules/zend.validator.html) to manage the fields validation in its forms.

```
$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

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

if($form->isValid()){

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

}
```

### Rendering Forms

[](#rendering-forms)

```

```

Outputs:

```

  firstName

  Error message

```

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

```

```

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

```
