respect-validation/README.md

117 lines
3.1 KiB
Markdown
Raw Normal View History

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
----------------
<?php
2011-02-07 02:35:35 +01:00
use Respect\Validation\Validator as v;
Simple validation
-----------------
$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()
->length(1,15);
2011-02-07 02:35:35 +01:00
$validUsername->validate('alganet'); //true
2011-02-07 02:35:35 +01:00
Validating object attributes
----------------------------
$validUser = v::attribute('username', $validUsername) //reusing!
->attribute('birthdate', v::date('Y-m-d'));
2011-02-07 02:35:35 +01:00
$user = new \stdClass;
$user->username = 'alganet';
2011-02-07 02:35:35 +01:00
$user->birthdate = '1987-07-01';
$validUser->validate($user); //true
2011-02-07 02:35:35 +01:00
Validator reuse (works on nested, big validators too!)
------------------------------------------------------
$validUsername->validate('respect'); //true
$validUsername->validate('alexandre gaigalas'); //false
$validUsername->validate('#$%'); //false
2011-02-07 02:35:35 +01:00
Cool, informative exceptions
----------------------------
The following code:
2011-02-07 02:35:35 +01:00
try {
$validUsername->assert('really messed up screen#name');
2011-02-07 02:35:35 +01:00
} catch(\InvalidArgumentException $e) {
echo $e->getFullMessage();
}
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
------------------------------------
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 {
$validBlogPost->assert($blogPost);
2011-02-07 02:35:35 +01:00
} catch (\InvalidArgumentException $e) {
echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage();
2011-02-07 02:35:35 +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)