Go to file
2011-02-06 22:44:42 -02:00
library New README, fixes on positive and negative validator messages 2011-02-06 20:51:50 -02:00
tests New README, fixes on positive and negative validator messages 2011-02-06 20:51:50 -02:00
.gitignore Rules for types: array, object, instances 2010-11-08 22:37:50 -02:00
README.textile README fix 2011-02-06 22:44:42 -02:00

h2. 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

h2. Quick Reference:

h3. Namespace import:

<pre> use Respect\Validation\Validator as v;</pre>

h3. Simple validation

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

h3. Chained validation

<pre>//From 1 to 15 non-whitespace alphanumeric characters 
$username = 'alganet';
$validUsername = v::alnum()
                  ->noWhitespace()
                  ->length(1,15)
                  ->validate($username);</pre>
<pre>
//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);</pre>

h3. Validating object attributes

<pre>$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'));</pre>

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

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

h3. Cool, informative exceptions:

<pre>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();
}</pre>

h3. Find specific messages inside nested validation exceptions

<pre>$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();
}</pre>

h3. Using Zend and/or Symfony validators

<pre>$valid = v::zend('hostname')->assert('google.com');</pre>
<pre>$valid = v::sf('time')->assert('22:00:01');</pre>
 
Cool, isn't it?

h2. Roadmap

* Custom validators (create your own validation rules and exceptions)
* Validation message improvements (translation, contextualization)