Koine Validator
Validator adapter for letting you use whathever awesome validation lib you want., (*1)
Code information:, (*2)
, (*3)
Package information:, (*4)
, (*5)
Usage
In order to create a validator, extend the executeValidation method:, (*6)
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:, (*7)
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');
}
}
}
Installing
Via Composer
Append the lib to your requirements key in your composer.json., (*8)
{
// composer.json
// [..]
require: {
// append this line to your requirements
"koine/validator": "~0.9.0"
}
}
Alternative install
Issues/Features proposals
Here is the issue tracker., (*9)
Contributing
Please refer to the contribuiting guide., (*10)
Lincense
MIT, (*11)
Authors