Provides set of standard validation methods and an input validation engine.
The Validator class can be used to quickly validate a single piece of input.
use werx\Validation\Validator;
$input = 'foo';
$valid = Validator::minlength($input, 4);
var_dump($valid);
/*
bool(true)
*/
The following validators are available. Each validator returns a bool. true
= passed validation, false
= failed validation.
bool required (mixed $input)
bool date (mixed $input [, $input_format = MM/DD/YYYY])
# Other input formats available YYYY/MM/DD, YYYY-MM-DD, YYYY/DD/MM, YYYY-DD-MM, DD-MM-YYYY, DD/MM/YYYY, MM-DD-YYYY, MM/DD/YYYY, YYYYMMDD, YYYYDDMM
bool minlength(mixed $input, int $min)
bool maxlength(mixed $input, int $max)
bool exactlength(mixed $input, int $length)
bool greaterthan($input, int $min)
bool lessthan(mixed $input, int $max)
bool alpha(mixed $input)
bool alphanumeric(mixed $input)
bool integer(mixed $input)
bool float(mixed $input)
bool numeric(mixed $input)
bool email(mixed $input)
bool url(mixed $input)
bool phone(mixed $input)
bool zipcode(mixed $input)
bool startswith(mixed $input, string $match)
bool endswith(mixed $input, string $match)
bool contains(mixed $input, string $match)
bool regex(mixed $input, string $regex)
bool inlist(mixed $input, array $list)
The Validation Engine is used to validate a set of data against a set of rules.
First, get an instance of the Validation Engine:
use werx\Validation\Engine as ValidationEngine;
$validator = new ValidationEngine;
Then add rules:
$validator->addRule('firstname', 'First Name', 'required|minlength[2]|alpha');
minlength[2]
inlist{red,white,blue}
required
validator, all validators will return true if the input is empty.
minlength[2]
will only actually fire if you also add a required
rule.Now you can get a validation result.
$valid = $validator->validate($_POST);
Sometimes you aren’t using a simple string as your input field name. Let’s say your HTML input form is something like this:
<input type="text" name="volunteer[name]">
<input type="text" name="volunteer[email]">
To build a rule for in this scenario, separate the array name and key name with a period when adding your rule.
$validator->addRule('volunteer.name', 'Name', 'required|minlength[2]|alpha');
$validator->addRule('volunteer.email', 'Email Address', 'required|email');
In addition to predefined validation methods from the Validator
class, you can also use closures to create custom validation methods.
$closure = function ($data, $id, $label) {
$message = null;
$success = $data[$id] == 'Foo';
if (!$success) {
$message = sprintf('%s must equal "Foo"', $label);
}
return [$success, $message];
};
$validator->addRule('firstname', 'First Name', $closure);
$valid = $validator->validate($_POST);
Three values will be passed to your closure:
The closure is expected to return an array.
bool
).What if you want to save groups of rules instead of adding each rule individually every time you want to validate them? We’ve got you covered.
Create a new class that extends werx\Validation\Ruleset
and add your rules in the constructor.
namespace your\namespace\Rulesets;
use werx\Validation\Ruleset;
class Contact extends Ruleset
{
public function __construct()
{
$this->addRule('firstname', 'First Name', 'required|minlength[2]');
$this->addRule('lastname', 'Last Name', 'required');
$this->addRule('phone', 'Phone Number', 'required|phone');
$this->addRule('email', 'Email Address', 'required|email');
}
}
Then when you are ready to validate this group of rules:
$contact_rules = new your\namespace\Rulesets\Contact;
$validator->addRuleset($contact_rules);
$valid = $validator->validate();
There are a couple utilities to make dealing with validation results easier.
Returns a simple array containing a list of validation error messages.
if (!$valid) {
$summary = $validator->getErrorSummary();
}
/*
Array
(
[0] => First Name must only contain the letters A-Z.
[1] => First Name must be at least 2 characters long.
[2] => Last Name is a required field.
)
*/
Returns the error summary formatted as an html unordered list (<ul>
).
Returns list of fields that had an error. Useful if you want to apply some decoration to your form indicating which fields had a validation errors.
if (!$valid) {
$error_fields = $validator->getErrorFields();
}
/*
Array
(
[0] => firstname
[1] => lastname
)
*/
Once you’ve added your rules, you can get back a list of required fields. This is useful when you want to indicate on your form which fields must be completed.
$validator->addRule('firstname', 'First Name', 'required');
$validator->addRule('lastname', 'Last Name', 'required');
$validator->addRule('age', 'Age', 'required|integer');
$required = $validator->getRequiredFields();
/*
Array
(
[0] => firstname
[1] => lastname
[2] => age
)
*/
Allows you to set custom error messages.
When displaying the error messages, {name}
will be replaced with the name of the field being validated. The rest of the field
is parsed with sprintf()
so that parameters like minlength
can be placed in the returned error message.
Examples:
$validator->addCustomMessage('required', "You didn't provide a value for {name}!");
$validator->addCustomMessage('minlength', "Oops, {name} must be at least %d characters long.");
This package is installable and autoloadable via Composer as werx/validation. If you aren’t familiar with the Composer Dependency Manager for PHP, you should read this first.
$ composer require werx/validation --prefer-dist