, (*1)
Library to validate form data based on rules., (*2)
Install
Via Composer, (*3)
$ composer require forestry/form-validator
Usage
Create a validator
$validator = new Forestry\FormValidator\Validator();
$rules = [
'name' => 'alpha|required',
'age' => 'num|required',
];
$validator->validate($_POST, $rules);
if ($validator->hasErrors()) {
$errors = $validator->getErrors();
}
Show error message
<label for="name">Name</label>
<input type="text" name="name" id="name">
<?= (!empty(Forestry\FormValidator\Validator::error('name')) ? Forestry\FormValidator\Validator::error('name') : '') ?>
You can also wrap error messages with custom markup:, (*4)
Forestry\FormValidator\Validator::error('confirm_password', '<span class="text-danger">{message}</span>');
Rules
This package comes with the following built-in rules:, (*5)
| Rule |
Parameter |
Description |
| alpha |
none |
Allows only text, space, . and - |
| alphanum |
none |
Allows text and numerical values |
| boolean |
none |
Checks if the value can be interpreted as a boolean true (true, 1, yes or on). |
| date |
format |
Checks if the value is a valid date, time or date-time string. You can provide a date format to check against. |
| email |
none |
Checks if the value is a valid email address. |
| float |
none |
Checks if the value is a valid float value. |
| integer |
none |
Checks if the value is a valid integer value. |
| ip |
none |
Checks if the value is a valid IP address. |
| max |
number |
Checks if the value exceeds number characters. |
| min |
number |
Checks if the value has at least number characters. |
| natural |
none |
Checks if the value is a natural number. |
| number |
none |
Checks if the value is a number. |
| phone |
none |
Checks if the value is a valid phone number format. |
| required |
none |
Checks if the value is set. |
| same |
fieldname |
Compares if the value matches with the one in fieldname. |
| url |
none |
Checks if the value is a valid URL. |
same rule example
With this rule, you can define that two fields must have the same value, e.g. password fields:, (*6)
$rules['password'] = 'required|min:8';
$rules['confirm_password'] = 'same:password';
Custom messages
Instead of using the default error messages, you can also pass a custom one with each rule:, (*7)
$rules['tos'] = "required--Please accept the terms of service";
Custom rules
You can define your own rules:, (*8)
First, create a class which implements the Forestry\FormValidator\SimpleRuleInterface or Forestry\FormValidator\ParameterRuleInterface. The later on is used for rules whose have a parameter., (*9)
use Forestry\FormValidator\SimpleRuleInterface;
MyRule implements SimpleRuleInterface
{
public function validate($value)
{
// Your code.
}
public function getMessage($value)
{
// Your code.
}
}
Next, register your rule with a name and the full qualified class name., (*10)
$validator->registerRule('myrule', '\MyRule');
If you want to change an already registered rule, use the updateRule() method., (*11)
$validator->updateRule('url', '\MyRule');
Testing
bash
$ phpunit, (*12)
Contributing
Please see CONTRIBUTING for details., (*13)
Credits
License
The MIT License (MIT). Please see License File for more information., (*14)