Skip to content

Validator adapter for using whathever awesome validtion library you want

License

Notifications You must be signed in to change notification settings

koinephp/Validator

Repository files navigation

Koine Validator

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

About

Validator adapter for using whathever awesome validtion library you want

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages