koine/validator

Validation adapter

0.9.0 2014-09-26 19:14 UTC

This package is not auto-updated.

Last update: 2024-11-09 17:50:45 UTC


README

Validator adapter for letting you use whathever awesome validation lib you want.

Code information:

Build Status Coverage Status Code Climate Scrutinizer Code Quality

Package information:

Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

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

Issues/Features proposals

Here is the issue tracker.

Contributing

Please refer to the contribuiting guide.

Lincense

MIT

Authors