Go to file
2010-12-06 19:20:13 -02:00
library StringLength refactoring (i'm very proud of this one!) 2010-12-06 19:20:13 -02:00
tests Refactoring on AbstractRelated, Call validator for validating inputs after passing them for a callback 2010-12-06 19:00:28 -02:00
.gitignore Rules for types: array, object, instances 2010-11-08 22:37:50 -02:00
README.textile README update 2010-11-30 13:42:58 -02:00

h2. About

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

* Fluent/Chained Interfaces (the "jquery" interface)
* Consistent API
* Composite validation (one of, all of, most of, at least N of, none of, etc...)
* Extensibility (create your own validators using the same engine)
* Solid objects for raw instantiation (tests, dependency injection, etc...)
* Informative, usable exceptions
* IDE autocomplete-readiness
* Integration with Zend 2.0 and Symfony 2.0 validators

Here some nice little things you can do using it:

h3. Namespace import:

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

h3. Simple validation

<pre> v::numeric()->validate($someNumber); </pre>

h3. Chained validation

<pre>//From 1 to 15 non-whitespace alphanumeric characters 
$username = 'alganet';
$validUsername = v::alnum()
                  ->noWhitespace()
                  ->stringLength(1,15)
                  ->validate($username);
</pre>
<pre>
//Date between two ranges using a specific format
$someDate = '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::all(
    v::hasAttribute('name', v::notEmpty()),
    v::hasAttribute('birthdate, v::date('Y-m-d'))
);</pre>

h3. Combining rules (v::oneOf, v::allOf, v::atLeast, v::mostOf, v::noneOf)

<pre>$validDate = v::oneOf(
    v::date('Y-m-d'),
    v::date('y-m-d'), //two-digit year
    v::date('d/m/Y')  
);</pre>

h3. Compositing rules

<pre>//Must satisfy at least two of the three conditions:
//a) A numeric or alphanumeric value
//b) A date in the format Ymd (20091009)
//c) A string between 3 and 12 characters without whitespace
$valid = v::atLeast(2, array(
   v::oneOf(v::numeric(), v::alnum()),
   v::date('Ymd'),
   v::stringLength(3,12)->noWhitespace()
));</pre>

h3. Cool, informative exceptions:

<pre>try {
    $username = '#$%  #odjfubgihdbfgihbdfighb';
    $validUsername = v::alnum('_')
                      ->noWhitespace()
                      ->stringLength(1,15)
                      ->assert($username);
} catch(v\Exceptions\InvalidException $e) {
   /*
       Respect\Validation\Exceptions\InvalidException: 
       #$%  #odjfubgihdbfgihbdfighb does not contains only letters and digits (including _)
       #$%  #odjfubgihdbfgihbdfighb contains spaces, tabs, line breaks or other not allowed charaters.
       #$%  #odjfubgihdbfgihbdfighb exceeds the maximum of 15 characters
   */
   echo $e->getMessage();
}</pre>
 
h3. Non-fluent interface for Dependency Injection maniacs

<pre>$validator = new v\Rules\Numeric();
$validator->validate(123);</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?