simettric / simple-form
A simple way to working with forms in php.
Installs: 1 888
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 2
Forks: 3
Open Issues: 0
Requires
- php: >=5.4
- zendframework/zend-validator: 2.2.10
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2025-01-24 23:48:23 UTC
README
A simple way to working with forms in php.
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.
$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
<?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>
*Note: you can return a null value in your Form::getName() method in order to set a clean input name like:
<input type="text" name="firstName" required="required">
*Note2: using Field::getRow("label") method, the result is similar but with a custom label.
<?php echo $form["firstName"]->getRow('Text for the label tag') ?>
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 ){} ?>