koine / validator
Validation adapter
0.9.0
2014-09-26 19:14 UTC
Requires
- php: >=5.3.3
- koine/core: ~0.9.10
Requires (Dev)
- phpunit/phpunit: *
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-12-21 18:24:46 UTC
README
Validator adapter for letting you use whathever awesome validation lib you want.
Code information:
Package information:
Usage
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'); } } }
Installing
Via Composer
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" } }
Alternative install
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Issues/Features proposals
Here is the issue tracker.
Contributing
Please refer to the contribuiting guide.