Simple, Object oriented forms for PHP.

1.0.3 2018-07-24 19:23 UTC

README

Rapid, themable forms for PHP.

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

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.

For legal information, check LICENSE and NOTICE.

README