Validator adapter for letting you use whathever awesome validation lib you want.
Code information:
Package information:
In order to create a validator, extend the executeValidation
method:
class UserValidator extends Validator
{
/**
* {@inheritdocs}
*/
protected function executeValidation($value)
{
if (!isset($value['name'])) {
$this->getErrors()->add('name', 'you must set name');
} elseif (!$value['name']) {
$this->getErrors()->add('name', 'name cannot be empty');
}
if (!isset($value['lastName'])) {
$this->getErrors()->add('lastName', 'you must set last name');
} elseif (!$value['lastName']) {
$this->getErrors()->add('lastName', 'last name cannot be empty');
}
}
}
$user = array(
'name' => 'Jon',
'lastName' => '',
);
$validator = new UserValidator();
$validator->isValid($user); // false
$validator->getErrors()->toArray();
// array('lastName' => array('last name cannot be empty'))
$user['lastName'] = 'Doe';
$validator->isValid($user); // true
If you also want to validate type of value, you can delegate validation to a typed method:
class UserValidator extends Validator
{
/**
* {@inheritdocs}
*/
protected function executeValidation($value)
{
$this->validateUser($value);
}
private function validateUser(array $user)
{
if (!isset($user['name'])) {
$this->getErrors()->add('name', 'you must set name');
} elseif (!$user['name']) {
$this->getErrors()->add('name', 'name cannot be empty');
}
if (!isset($user['lastName'])) {
$this->getErrors()->add('lastName', 'you must set last name');
} elseif (!$user['lastName']) {
$this->getErrors()->add('lastName', 'last name cannot be empty');
}
}
}
Append the lib to your requirements key in your composer.json.
{
// composer.json
// [..]
require: {
// append this line to your requirements
"koine/validator": "~0.9.0"
}
}
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Here is the issue tracker.
Please refer to the contribuiting guide.