2011-02-07 02:35:35 +01:00
|
|
|
Respect Validation
|
|
|
|
==================
|
|
|
|
|
|
|
|
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
|
|
|
|
----------------
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
<?php
|
2011-02-07 02:35:35 +01:00
|
|
|
use Respect\Validation\Validator as v;
|
|
|
|
|
|
|
|
Simple validation
|
|
|
|
-----------------
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
$number = 123;
|
|
|
|
v::numeric()->validate($number); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
|
|
|
Chained validation
|
|
|
|
------------------
|
|
|
|
|
|
|
|
//From 1 to 15 non-whitespace alphanumeric characters
|
|
|
|
$validUsername = v::alnum()
|
|
|
|
->noWhitespace()
|
2011-02-07 15:22:30 +01:00
|
|
|
->length(1,15);
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
$validUsername->validate('alganet'); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
|
|
|
Validating object attributes
|
|
|
|
----------------------------
|
2011-02-07 15:22:30 +01:00
|
|
|
|
|
|
|
$validUser = v::attribute('username', $validUsername) //reusing!
|
|
|
|
->attribute('birthdate', v::date('Y-m-d'));
|
2011-02-07 02:35:35 +01:00
|
|
|
|
|
|
|
$user = new \stdClass;
|
2011-02-07 15:22:30 +01:00
|
|
|
$user->username = 'alganet';
|
2011-02-07 02:35:35 +01:00
|
|
|
$user->birthdate = '1987-07-01';
|
2011-02-07 15:22:30 +01:00
|
|
|
|
|
|
|
$validUser->validate($user); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
|
|
|
Validator reuse (works on nested, big validators too!)
|
|
|
|
------------------------------------------------------
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
$validUsername->validate('respect'); //true
|
|
|
|
$validUsername->validate('alexandre gaigalas'); //false
|
|
|
|
$validUsername->validate('#$%'); //false
|
2011-02-07 02:35:35 +01:00
|
|
|
|
|
|
|
Cool, informative exceptions
|
|
|
|
----------------------------
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
The following code:
|
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
try {
|
2011-02-07 15:22:30 +01:00
|
|
|
$validUsername->assert('really messed up screen#name');
|
2011-02-07 02:35:35 +01:00
|
|
|
} catch(\InvalidArgumentException $e) {
|
|
|
|
echo $e->getFullMessage();
|
|
|
|
}
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
Produces this message:
|
|
|
|
|
|
|
|
\-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
|
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
Message finding on nested Exceptions
|
|
|
|
------------------------------------
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
Consider the following scenario:
|
|
|
|
|
|
|
|
$validBlogPost = v::object()
|
|
|
|
->attribute('title', v::string()->length(1,32))
|
|
|
|
->attribute('author', $validUser) //reuse!
|
|
|
|
->attribute('date', v::date())
|
|
|
|
->attribute('text', v::string());
|
|
|
|
|
|
|
|
$blogPost = new \stdClass;
|
|
|
|
$blogPost->author = clone $validUser;
|
|
|
|
$blogPost->author->username = '# invalid #';
|
|
|
|
|
|
|
|
The following code:
|
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
try {
|
2011-02-07 15:22:30 +01:00
|
|
|
$validBlogPost->assert($blogPost);
|
2011-02-07 02:35:35 +01:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
2011-02-07 15:22:30 +01:00
|
|
|
echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage();
|
2011-02-07 02:35:35 +01:00
|
|
|
}
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
Finds the specific noWhitespace message inside author->username and prints it:
|
|
|
|
|
|
|
|
>"# invalid #" contains whitespace
|
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
Using Zend and/or Symfony validators
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
$validHostName = v::zend('hostname')->assert('google.com');
|
|
|
|
$validTime = 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)
|
|
|
|
|
|
|
|
|