A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.
A simple, extensible validation library for PHP with support
for filtering and validating any input array along with
generating client side validation code.
This library provides a simple way to validate an input
array against a set of rules. Input could come from $_POST
or any other data source.
Each field can have its own label, pre-filters and rules
applied to it. Rules extend a very simple interface, making
adding custom rules very easy. The Validator object itself
can be executed multiple times against different datasets,
making it very useful for processing dynamic data.
Additionally, validation rules can be generated for client
side scripts. Currently only jQuery Validate is supplied
but additional interfaces can be added easily.
use HybridLogic\Validation\Validator;
use HybridLogic\Validation\Rule;
$validator = new Validator();
$validator
->set_label('name', 'first name')
->set_label('email', 'email address')
->set_label('password2', 'password confirmation')
->add_filter('name', 'trim')
->add_filter('email', 'trim')
->add_filter('email', 'strtolower')
->add_rule('name', new Rule\MinLength(5))
->add_rule('name', new Rule\MaxLength(10))
->add_rule('email', new Rule\MinLength(5))
->add_rule('email', new Rule\Email())
->add_rule('password', new Rule\Matches('password2'))
;
if($validator->is_valid($_POST)) {
print_r($validator->get_data());
} else {
print_r($validator->get_errors());
}
More detailed examples can be found in ./examples.
phpunit tests
@todo