Wallogit.com
2017 © Pedro Peláez
Object oriented validation library for PHP
Validus is a PHP library for object/array data validation. Main goal was to provide users with easy to use and extend software, which can be easily integrated in their own projects. Validus is relying on modern PHP (>=5.3.0) functionality and concepts, such as namespaces and PSR-0 autoloading., (*1)
Given simple std object, such as, (*2)
$user = new stdClass(); $user->name = 'John'; $user->age = 25; $user->email = 'john@doe.com'; $user->secondEmail = 'john@gmail.com';
and Validus instantiated (don't forget autoloading), (*3)
$validation = new \Validus\Validus();
you can apply validation rules with on(), sameAs() or entire() methods:, (*4)
$validation->on('age')->lt(30, "Age can't exceed 30 years");
$validation->on('email')->email(null, "must complain with RFC 2821");
$validation->on('secondEmail')->sameAs('email');
$validation->entire($user)->notempty();
After that, validation check is straight forward:, (*5)
if($validation->fails()){
print_r($validation->errors());
}
else{
echo "User is valid\n";
}
Please refer to examples code for implementation details, here's short description of more advanced features that are available: + You can define closure as validation rule, (*6)
$validation->on('age')->closure(function($age){...});
$validation->on('name')->regexp('/^([a-zA-Z]*)$/', "Only characters a-zA-Z");