Skip to content
Developer experience focused validator.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitattributes
.gitignore
.scrutinizer.yml
.styleci.yml
.travis.yml
CHANGELOG.md
LICENSE.md
README.md
composer.json
phpunit.xml

README.md

Basic validator

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Installation

Via Composer

composer require albert221/validation ^2.0

Usage

use Albert221\Validation\Validator;
use Albert221\Validation\Rule;

// $data = [...];

$verdicts = Validator::build()
    ->addField('username')
        ->addRule(Rule\Required::class)
        ->addRule(Rule\Length::class, ['min' => 4])
        ->addRule(Rule\PdoUnique::class, ['pdo' => $pdo, 'table' => 'users', 'field' => 'username'])
    ->addField('email')
        ->addRule(Rule\Required::class)
        ->addRule(Rule\Email::class)
        ->addRule(Rule\PdoUnique::class, ['pdo' => $pdo, 'table' => 'users', 'field' => 'email'])
    ->addField('password')
        ->addRule(Rule\Required::class)
        ->addRule(Rule\Length::class)
            ->setOption('min', 6) // You can set options that way, too!
        ->addRule(Rule\Complexity::class, ['alpha' => true, 'num' => true, 'special' => true])
            ->setMessage('Your password is too weak!')
    ->addField('confirm_password')
        ->addRule(Rule\SameAs::class, ['field' => 'password']
    ->validate($data);

if ($verdicts->fails()) {
    // Validation failed
}

// Validation passed

# Other methods

$verdicts->passes(); // Is valid?
$verdicts->forField('username'); // Get all verdicts for specified field.
$verdicts->forField('username')->passes(); // Is specified field valid?
$verdicts->toArray(); // Get all verdicts as an array.
You can’t perform that action at this time.