seanmorris / form
Simple, Object oriented forms for PHP.
Installs: 3 236
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- seanmorris/theme: ^1.0
Requires (Dev)
- seanmorris/ids: dev-sm-api-tooling as dev-master
- simpletest/simpletest: ^1.1
README
Rapid, themable forms for PHP.
The Form library allows you to rapidly develop, theme, and validate forms, so you can get back to programming.
Creating a Form
Creating a form is simple. Elements starting with a letter or number are fields. Elements starting with underscores are special keys. For example _method
allows you to set the choose a GET or POST request for your form.
Elements starting with a letter or number are rendered directly into the <input> or <select> tag. Elements beginning with an underscore are passed to logic but not rendered.
For example, the _title attribute generates a <label tag for the form field.
$skeleton['_method'] = 'POST'; $skeleton['testField'] = [ 'type' => 'text' , '_title' => 'Test Field' ]; $skeleton['submit'] = [ '_title' => 'Submit' , 'type' => 'submit' ]; $form = new \SeanMorris\Form\Form($skeleton); echo $form->render();
Validation
Validators are specified on the _validators key. Its an array keyed by validator class. The values are arrays of arguments to pass to the constructor.
$skeleton['testField'] = [ 'type' => 'text' , '_title' => 'Test Field' , '_validators' => [ 'SeanMorris\Form\Validator\Regex' => [ '/.{8,}/' => '%s must be at least 8 characters' ] ] ];
Filtering and validating submitted values is simple:
$form->setValues($_POST); if($form->validate()) { // Values will only contain keys for each of the fields. $values = $form->getValues(); } else { $errors = $form->erorrs(); }
More...
For the field type list, validator list, and usage guide read DOCS.
For the guide to extending the library to create new field types, read EXTENDING.