Download the PHP package jridgewell/form-validator without Composer
On this page you can find all versions of the php package jridgewell/form-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jridgewell/form-validator
More information about jridgewell/form-validator
Files in jridgewell/form-validator
Package form-validator
Short Description A simple HTML form validator
License MIT
Homepage https://github.com/jridgewell/FormValidator
Informations about the package form-validator
FormValidator
FormValidator allows you to create and validate forms using a simple rule based approach. It uses an API very similar to Rails' ActiveRecord.
Basics
A form file is just a class that extends the \FormValidator\Form class
In this example, the form validator checks if name
isn't empty
test.form.php (the model)
index.php (the controller)
form.html.php (the view)
About the View
- If the form fails validation, by using the
$form->input
method, we preserve whatever value the user typed into that field (except for password fields) - The form must have a field with the name attribute set to the
name of the form class (
name="TestForm"
in our example). Using the$form->submit
method takes care of this requirement.
Installation
Via Composer
composer require "jridgewell/form-validator:1.*"
Then just add require 'vendor/autoload.php';
to any code that requires
FormValidator.
The Validations Array
The $validations
array contains all the form fields and rules that
need to pass, for the form to be valid. In the example above, it showed
a single rule applying to one form element, but you can apply multiple
rules to an element by using an array.
In our html file, if we wanted to show the errors for the validations, we could do the following:
Validation Array Options
Most validations also support passing in an options array. This allows for custom messages, and can allow for a field to be optional (blank). Please see the validation for acceptable parameters.
List of validations
Simple Validations
Validation | Options | Description |
---|---|---|
Validation::anything() |
No options |
This field is always valid |
Validation::acceptance() |
|
This field must be accepted (truthy) |
Validation::email() |
|
This field must be a valid email |
Validation::length() |
|
This field's number of characters must be in the supplied range. If no options are passed, this field will always be valid |
Validation::numericality() |
|
This field must be a number |
Validation::presence() |
|
This field must not be empty |
Validation::url() |
|
This field must be a valid url |
Advanced Validations (require parameters)
Validation | Parameter | Options | Description |
---|---|---|---|
Validation::confirmation($func) |
|
|
This field must match the return value of $other_field_func. Useful for confirming a password in a second field. |
Validation::exclusion($array) |
|
|
This field must not be equal (==) to a value inside $array. |
Validation::format($regex) |
|
|
This field must match against the supplied $regex |
Validation::inclusion($array) |
|
|
This field must be equal (==) to a value inside $array. |
Validation::validateWith($func) /td> |
|
|
This validation allows for a custom function to preform the field validation. It's return value must be (===) true, or else it will use the return value as the field's error message |
Advanced Validation Examples
Validation::confirmation($other_field_func)
Validation::exclusion($array)
Validation::format($regex)
Validation::inclusion($array)
Validation::validateWith($func)
This validation requires a (callable) callback. This callback is
then provided with the submitted field data as it's only parameter. The
callback can either return true
and the validation will pass, or
return anything else and the return will be used as the error message
for the field.