Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
The most awesome validation engine ever created for PHP
PHP
branch: master

This branch is 767 commits behind Respect:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
README.textile

README.textile

There’s nothing stable in the master branch yet. Check out the develop branch

About

Respect\Validation is the most awesome validation engine ever created for PHP. Featuring:

  • Fluent/Chained builders
  • Composite validation (nested, grouped and related rules)
  • Informative, awesome exceptions
  • More than 30 fully tested validators
  • PHP 5.3 only
  • Possible integration with Zend 2.0 and Symfony 2.0 validators

Quick Reference:

Namespace import:

 use Respect\Validation\Validator as v;

Simple validation

 v::numeric()->validate($someNumber); //returns true or false 

Chained validation

//From 1 to 15 non-whitespace alphanumeric characters 
$username = 'alganet';
$validUsername = v::alnum()
                  ->noWhitespace()
                  ->length(1,15)
                  ->validate($username);
//Date between two ranges using a specific format
$someDate = new DateTime('2010-10-15');
$validDate = v::date('Y-m-d')
              ->between(new DateTime('2009-01-01'), new DateTime('2011-01-01'))
              ->validate($someDate);

Validating object attributes

$user = new \stdClass;
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';
$validUser = v::attribute('name', v::notEmpty())
                ->v::attribute('birthdate, v::date('Y-m-d'));

Validator reuse (works on nested, big validators too!)

$idValidator = v::int()->positive();
$idValidator->validate(123); //true
$idValidator->validate(456); //true
$idValidator->validate('foo'); //false
$idValidator->validate(178); //true

Cool, informative exceptions:

try {
    $username = '#$%  #odjfubgihdbfgihbdfighb';
    $validUsername = v::alnum('_')
                      ->noWhitespace()
                      ->length(1,15)
                      ->assert($username);
} catch(\InvalidArgumentException $e) {
   /* prints:
        \-None of 3 required rules passed
          |-"really messed up screen#name" does not contain only letters, digits and "_"
          |-"really messed up screen#name" contains whitespace
          \-"really messed up screen#name" length is not between 1 and 15
   */
   echo $e->getFullMessage();
}

Find specific messages inside nested validation exceptions

$user = array("id" => "some %% invalid %% id");
$post = array("user" => $user);
try {
    v::key("user", v::key("id", v::int()->positive()))->assert($post);
} catch (\InvalidArgumentException $e) {
    /* prints:
        "some %% invalid %% id" is not a positive number
    */
    echo $e->findRelated('user', 'id', 'positive')->getMainMessage();
}

Using Zend and/or Symfony validators

$valid = v::zend('hostname')->assert('google.com');
$valid = v::sf('time')->assert('22:00:01');

Cool, isn’t it?

Roadmap

  • Custom validators (create your own validation rules and exceptions)
  • Validation message improvements (translation, contextualization)
Something went wrong with that request. Please try again.