mirror of
https://github.com/Respect/Validation.git
synced 2024-06-27 17:50:13 +02:00
New README, fixes on positive and negative validator messages
This commit is contained in:
parent
8098321bcd
commit
6c0573e171
|
@ -2,16 +2,14 @@ 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
|
||||
* 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
|
||||
|
||||
Here some nice little things you can do using it:
|
||||
Quick Reference:
|
||||
|
||||
h3. Namespace import:
|
||||
|
||||
|
@ -19,7 +17,7 @@ h3. Namespace import:
|
|||
|
||||
h3. Simple validation
|
||||
|
||||
<pre> v::numeric()->validate($someNumber); </pre>
|
||||
<pre> v::numeric()->validate($someNumber); //returns true or false </pre>
|
||||
|
||||
h3. Chained validation
|
||||
|
||||
|
@ -28,46 +26,30 @@ $username = 'alganet';
|
|||
$validUsername = v::alnum()
|
||||
->noWhitespace()
|
||||
->length(1,15)
|
||||
->validate($username);
|
||||
</pre>
|
||||
->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>
|
||||
->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::attribute('name', v::notEmpty()),
|
||||
v::attribute('birthdate, v::date('Y-m-d'))
|
||||
);</pre>
|
||||
$validUser = v::attribute('name', v::notEmpty())
|
||||
->v::attribute('birthdate, v::date('Y-m-d'));</pre>
|
||||
|
||||
h3. Combining rules (v::oneOf, v::allOf, v::atLeast, v::mostOf, v::noneOf)
|
||||
h3. Validator reuse (works on nested, big validators too!)
|
||||
|
||||
<pre>$validDate = v::oneOf(
|
||||
v::date('Y-m-d'),
|
||||
v::date('y m d'),
|
||||
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::string()->length(3,12)->noWhitespace()
|
||||
));</pre>
|
||||
<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:
|
||||
|
||||
|
@ -77,20 +59,28 @@ h3. Cool, informative exceptions:
|
|||
->noWhitespace()
|
||||
->length(1,15)
|
||||
->assert($username);
|
||||
} catch(v\Exceptions\InvalidException $e) {
|
||||
/*
|
||||
Respect\Validation\Exceptions\AllOfException: None of 3 required rules passed
|
||||
-"#$% #odjfubgihdbfgihbdfighb" does not contain only letters, digits and "_"
|
||||
-"#$% #odjfubgihdbfgihbdfighb" contains whitespace
|
||||
-"#$% #odjfubgihdbfgihbdfighb" length is not between 1 and 15
|
||||
} 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->getMessage();
|
||||
echo $e->getFullMessage();
|
||||
}</pre>
|
||||
|
||||
h3. Non-fluent interface for Dependency Injection maniacs
|
||||
|
||||
<pre>$validator = new v\Rules\Numeric();
|
||||
$validator->validate(123);</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
|
||||
|
||||
|
@ -99,4 +89,9 @@ h3. Using Zend and/or Symfony validators
|
|||
|
||||
Cool, isn't it?
|
||||
|
||||
h2. Roadmap
|
||||
|
||||
* Custom validators (create your own validation rules and exceptions)
|
||||
* Validation message improvements (translation, contextualization)
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ class NegativeException extends ValidationException
|
|||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
'"%d" is not a negative number',
|
||||
'"%s" is not a negative number',
|
||||
);
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ class PositiveException extends ValidationException
|
|||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
'"%d" is not a positive number',
|
||||
'"%s" is not a positive number',
|
||||
);
|
||||
|
||||
}
|
|
@ -164,7 +164,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
v::noWhitespace() //no whitespace allowed
|
||||
)->assert('alganet');
|
||||
|
||||
//same as
|
||||
//same as
|
||||
|
||||
v::string()
|
||||
->length(5, 20)
|
||||
|
@ -217,4 +217,24 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
|
||||
public function testReadme()
|
||||
{
|
||||
$username = 'really messed up screen#name';
|
||||
$validUsername = v::alnum('_')
|
||||
->noWhitespace()
|
||||
->length(1, 15);
|
||||
try {
|
||||
$validUsername->assert($username);
|
||||
} catch (\Exception $e) {
|
||||
//echo $e->getFullMessage();
|
||||
}
|
||||
$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) {
|
||||
//echo $e->findRelated('user', 'id', 'positive')->getMainMessage();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue