mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 21:32:14 +02:00
Switched from textile to markdown
This commit is contained in:
parent
cd6449ff81
commit
4b4390eb31
107
README.md
Normal file
107
README.md
Normal file
|
@ -0,0 +1,107 @@
|
|||
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
|
||||
----------------
|
||||
|
||||
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 = '#some%really*bad screen name';
|
||||
$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();
|
||||
}
|
||||
|
||||
Message finding on nested 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
|
||||
------------------------------------
|
||||
|
||||
$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)
|
||||
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
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)
|
||||
|
||||
|
Loading…
Reference in a new issue