2011-11-28 14:49:27 +01:00
|
|
|
Respect\Validation [![Build Status](https://secure.travis-ci.org/Respect/Validation.png)](http://travis-ci.org/Respect/Validation)
|
2011-02-07 02:35:35 +01:00
|
|
|
==================
|
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
The most awesome validation engine ever created for PHP.
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
- Fluent/Chained builders like `v::numeric()->positive()->between(1, 256)->validate($myNumber)` (more samples below)
|
2011-02-07 02:35:35 +01:00
|
|
|
- Informative, awesome exceptions
|
|
|
|
- More than 30 fully tested validators
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
Installation
|
2012-04-10 03:46:28 +02:00
|
|
|
------------
|
2011-02-08 23:10:58 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Packages available on [PEAR](http://respect.li/pear) and [Composer](http://packagist.org/packages/Respect/Validation)
|
2011-02-08 23:10:58 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Autoloading is [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) compatible.
|
2011-02-08 23:10:58 +01:00
|
|
|
|
2011-02-07 15:46:08 +01:00
|
|
|
Feature Guide
|
2012-04-10 03:46:28 +02:00
|
|
|
-------------
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Namespace import
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2011-02-07 15:46:08 +01:00
|
|
|
Respect\Validation is namespaced, but you can make your life easier by importing
|
|
|
|
a single class into your context:
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
<?php
|
2011-02-07 02:35:35 +01:00
|
|
|
use Respect\Validation\Validator as v;
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Simple validation
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2011-02-07 15:46:08 +01:00
|
|
|
The Hello World validator is something like this:
|
|
|
|
|
2011-02-07 15:22:30 +01:00
|
|
|
$number = 123;
|
|
|
|
v::numeric()->validate($number); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Chained validation
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
It is possible to use validators in a chain. Sample below validates a string
|
|
|
|
containing numbers and letters, no whitspace and length between 1 and 15.
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
$usernameValidator = v::alnum()->noWhitespace()->length(1,15);
|
|
|
|
$usernameValidator->validate('alganet'); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Validating object attributes
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Given this simple object:
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
$user = new stdClass;
|
|
|
|
$user->name = 'Alexandre';
|
2011-02-07 02:35:35 +01:00
|
|
|
$user->birthdate = '1987-07-01';
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Is possible to validate its attributes in a single chain:
|
|
|
|
|
|
|
|
$userValidator = v::attribute('name', v::string()->length(1,32))
|
|
|
|
->attribute('birthdate', v::date()->minimumAge(18));
|
|
|
|
|
|
|
|
$userValidator->validate($user); //true
|
|
|
|
|
|
|
|
Validating array keys is also possible using `v::key()`
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Note that we used `v::string()` and `v::date()` in the beginning of the validator.
|
|
|
|
Although is not mandatory, it is a good practice to use the type of the
|
|
|
|
validated object as the first node in the chain.
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Negating rules
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
You can use the `v::not()` to negate any rule:
|
|
|
|
|
|
|
|
v::not(v::int())->validate(10); //false, input must not be integer
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Validator reuse
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Once created, you can reuse your validator anywhere. Remember $usernameValidator?
|
|
|
|
|
|
|
|
$usernameValidator->validate('respect'); //true
|
|
|
|
$usernameValidator->validate('alexandre gaigalas'); //false
|
|
|
|
$usernameValidator->validate('#$%'); //false
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Cool, informative exceptions
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
When something goes wrong, Validation can tell you exacty what's going on. For this,
|
|
|
|
we use the `assert()` method instead of `validate()`:
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
try {
|
2012-04-09 00:08:39 +02:00
|
|
|
$usernameValidator->assert('really messed up screen#name');
|
2011-02-07 02:35:35 +01:00
|
|
|
} catch(\InvalidArgumentException $e) {
|
|
|
|
echo $e->getFullMessage();
|
|
|
|
}
|
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
The printed message is exactly this, as a text tree:
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2011-02-08 23:10:58 +01:00
|
|
|
\-All of the 3 required rules must pass
|
2012-04-09 00:08:39 +02:00
|
|
|
|-"really messed up screen#name" must contain only letters (a-z) and digits (0-9)
|
2011-02-08 23:10:58 +01:00
|
|
|
|-"really messed up screen#name" must not contain whitespace
|
|
|
|
\-"really messed up screen#name" must have a length between 1 and 15
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Getting specific messages
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
The text tree is fine, but unusable on a HTML form or something more custom. You can use
|
|
|
|
`findMessages()` for that:
|
|
|
|
|
|
|
|
try {
|
|
|
|
$usernameValidator->assert('really messed up screen#name');
|
|
|
|
} catch(\InvalidArgumentException $e) {
|
|
|
|
var_dump($e->findMessages('alnum', 'length', 'noWhitespace'));
|
|
|
|
}
|
|
|
|
|
|
|
|
`findMessages()` returns an array with messages from the requested validators.
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Customizing messages
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Getting messages as an array is fine, but sometimes you need to customize them in order
|
|
|
|
to present them to the user. This is possible using the `findMessages()` method as well:
|
|
|
|
|
|
|
|
$errors = $e->findMessages(
|
|
|
|
'alnum' => '{{name}} must contain only letters and digits',
|
|
|
|
'length' => '{{name}} must not have more than 15 chars',
|
|
|
|
'noWhitespace' => '{{name}} cannot contain spaces'
|
|
|
|
);
|
|
|
|
|
|
|
|
For all messages, the `{{name}}` and `{{input}}` variable is available for templates.
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Validator name
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
On `v::attribute()` and `v::key()`, `{{name}}` is the attribute/key name. For others,
|
|
|
|
is the same as the input. You can customize a validator name using:
|
|
|
|
|
|
|
|
v::date('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
|
2011-02-07 15:46:08 +01:00
|
|
|
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Using Zend and/or Symfony validators
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
It is also possible to reuse validators from other frameworks if they are installed:
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
$hostnameValidator = v::zend('Hostname')->assert('google.com');
|
|
|
|
$timeValidator = v::sf('Time')->assert('22:00:01');
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Validation methods
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
We've seen `validate()` that returns true or false and `assert()` that throws a complete
|
|
|
|
validation report. There is also a `check()` method that returns an Exception
|
|
|
|
only with the first error found:
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2011-02-07 02:35:35 +01:00
|
|
|
try {
|
2012-04-09 00:08:39 +02:00
|
|
|
$usernameValidator->check('really messed up screen#name');
|
|
|
|
} catch(\InvalidArgumentException $e) {
|
|
|
|
echo $e->getMainMessage();
|
2011-02-07 02:35:35 +01:00
|
|
|
}
|
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Message:
|
|
|
|
|
|
|
|
"really messed up screen#name" must contain only letters (a-z) and digits (0-9)
|
|
|
|
|
|
|
|
Reference
|
2012-04-10 03:46:28 +02:00
|
|
|
---------
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Type validators
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::arr()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::bool()
|
|
|
|
* v::date()
|
|
|
|
* v::float()
|
|
|
|
* v::hexa()
|
|
|
|
* v::instance()
|
|
|
|
* v::int()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::nullValue()
|
|
|
|
* v::numeric()
|
|
|
|
* v::object()
|
|
|
|
* v::string()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Building Blocks
|
2012-04-09 00:55:05 +02:00
|
|
|
|
|
|
|
* v::call()
|
|
|
|
* v::callback()
|
|
|
|
* v::not()
|
|
|
|
* v::when()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Comparison validators
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
* v::between()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::equals()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::max()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::min()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Numeric related
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
* v::between()
|
|
|
|
* v::bool()
|
|
|
|
* v::even()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::float()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::hexa()
|
|
|
|
* v::int()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::multiple()
|
|
|
|
* v::negative()
|
|
|
|
* v::notEmpty()
|
|
|
|
* v::numeric()
|
|
|
|
* v::odd()
|
|
|
|
* v::perfectSquare()
|
|
|
|
* v::positive()
|
|
|
|
* v::primeNumber()
|
|
|
|
* v::roman()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### String related
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
* v::alnum()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::alpha()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::between()
|
|
|
|
* v::consonants()
|
|
|
|
* v::contains()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::digits()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::endsWith()
|
|
|
|
* v::in()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::length()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::lowercase()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::notEmpty()
|
|
|
|
* v::noWhitespace()
|
|
|
|
* v::regex()
|
|
|
|
* v::slug()
|
|
|
|
* v::startsWith()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::uppercase()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::uppercase()
|
|
|
|
* v::version()
|
|
|
|
* v::vowels()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Array/Object related
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
* v::arr()
|
|
|
|
* v::attribute()
|
|
|
|
* v::contains()
|
|
|
|
* v::each()
|
|
|
|
* v::endsWith()
|
|
|
|
* v::in()
|
|
|
|
* v::instance()
|
|
|
|
* v::key()
|
|
|
|
* v::length()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::notEmpty()
|
|
|
|
* v::startsWith()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Date related
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
* v::between()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::date()
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::leapDate()
|
|
|
|
* v::leapYear()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Group related
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::allOf()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::noneOf()
|
|
|
|
* v::oneOf()
|
2011-02-07 15:22:30 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Other
|
2011-03-31 00:06:59 +02:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::cnpj()
|
|
|
|
* v::cpf()
|
|
|
|
* v::domain()
|
|
|
|
* v::email()
|
|
|
|
* v::ip()
|
|
|
|
* v::json()
|
|
|
|
* v::macAddress()
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::sf()
|
|
|
|
* v::tld()
|
|
|
|
* v::zend()
|
2011-03-31 00:06:59 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
### Alphabetically
|
2011-04-29 21:00:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::allOf($v1, $v2, $v3...)
|
2011-03-31 00:06:59 +02:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Will validate if all inner validators validates.
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::allOf(
|
|
|
|
v::int(),
|
|
|
|
v::positive()
|
|
|
|
)->validate(15); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
This is similar to the chain (which is an allOf already), but
|
|
|
|
its syntax allows you to set custom names for every node:
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::allOf(
|
|
|
|
v::int()->setName('Account Number'),
|
|
|
|
v::positive()->setName('Higher Than Zero')
|
|
|
|
)->setName('Positive integer')
|
|
|
|
->validate(15); //true
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::oneOf() - Validates if at least one inner rule pass
|
|
|
|
* v::noneOf() - Validates if no inner rules pass
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::when() - A Ternary validator
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::alnum()
|
|
|
|
#### v::alnum(string $additionalChars)
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates alphanumeric characters from a-Z and 0-9.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::alnum()->validate('foo 123'); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
A parameter for extra characters can be used:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::alnum('-')->validate('foo - 123'); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
This validator allows whitespace, if you want to
|
|
|
|
remove them add `->noWhitespace()` to the chain:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::alnum()->noWhitespace->validate('foo 123'); //false
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
This validator also allows empty values, if you want
|
|
|
|
to invalidate them, add `->notEmpty()` to the chain:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::alnum()->notEmpty()->validate(''); //false
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
You can restrict case using the `->lowercase()` and
|
|
|
|
`->uppercase()` validators:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::alnum()->uppercase()->validate('aaa'); //false
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Message template for this validator includes `{{additionalChars}}` as
|
|
|
|
the string of extra chars passed as the parameter.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::alpha() - a-Z, empty or whitespace only
|
|
|
|
* v::digits() - 0-9, empty or whitespace only
|
|
|
|
* v::consonants()
|
|
|
|
* v::vowels()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::alpha()
|
|
|
|
#### v::alpha(string $additionalChars)
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
This is similar to v::alnum(), but it doesn't allow numbers. It also
|
|
|
|
accepts empty values and whitespace, so use `v::notEmpty()` and
|
|
|
|
`v::noWhitespace()` when appropriate.
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 02:35:35 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::alnum() - a-z0-9, empty or whitespace only
|
|
|
|
* v::digits() - 0-9, empty or whitespace only
|
|
|
|
* v::consonants()
|
|
|
|
* v::vowels()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::arr()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates if the input is an array or traversable object.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::arr()->validate(array()); //true
|
|
|
|
v::arr()->validate(new ArrayObject); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::each() - Validates each member of an array
|
|
|
|
* v::key() - Validates a specific key of an array
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::attribute($name)
|
|
|
|
#### v::attribute($name, v $validator)
|
|
|
|
#### v::attribute($name, v $validator, boolean $mandatory=true)
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates an object attribute.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
$obj = new stdClass;
|
|
|
|
$obj->foo = 'bar';
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::attribute('foo')->validate($obj); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
You can also validate the attribute itself:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::attribute('foo', v::equals('bar'))->validate($obj); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Third parameter makes the attribute presence optional:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::attribute('lorem', v::string(), false)->validate($obj); // true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
The name of this validator is automatically set to the attribute name.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::key() - Validates a specific key of an array
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::between($start, $end)
|
|
|
|
#### v::between($start, $end, boolean $inclusive=false)
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates ranges. Most simple example:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::int()->between(10, 20)->validate(15); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
The type as the first validator in a chain is a good practice,
|
|
|
|
since between accepts many types:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::string()->between('a', 'f')->validate('c'); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Also very powerful with dates:
|
2011-02-21 01:53:21 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::date()->between('2009-01-01', '2013-01-01')->validate('2010-01-01'); //true
|
2011-02-21 01:53:21 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Date ranges accept strtotime values:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::date()->between('yesterday', 'tomorrow')->validate('now'); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
A third parameter may be passed to validate the passed values inclusive:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::date()->between(10, 20, true)->validate(20); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Message template for this validator includes `{{minValue}}` and `{{maxValue}}`.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
See also:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
* v::length() - Validates the length of a input
|
|
|
|
* v::min()
|
|
|
|
* v::max()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::bool()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates if the input is a boolean value:
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::bool()->validate(true); //true
|
|
|
|
v::bool()->validate(false); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::call(callable $callback)
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
This is a very low level validator. It calls a function, method or closure
|
|
|
|
for the input and then validates it. Consider the following variable:
|
|
|
|
|
|
|
|
$url = 'http://www.google.com/search?q=respect.github.com'
|
|
|
|
|
|
|
|
To validate every part of this URL we could use the native `parse_url`
|
|
|
|
function to break its parts:
|
|
|
|
|
|
|
|
$parts = parse_url($url);
|
|
|
|
|
|
|
|
This function returns an array containing `scheme`, `host`, `path` and `query`.
|
|
|
|
We can validate them this way:
|
|
|
|
|
|
|
|
v::arr()->key('scheme', v::startsWith('http'))
|
|
|
|
->key('host', v::domain())
|
|
|
|
->key('path', v::string())
|
|
|
|
->key('query', v::notEmpty());
|
|
|
|
|
|
|
|
Using `v::call()` you can do this in a single chain:
|
|
|
|
|
|
|
|
v::call(
|
|
|
|
'parse_url',
|
|
|
|
v::arr()->key('scheme', v::startsWith('http'))
|
|
|
|
->key('host', v::domain())
|
|
|
|
->key('path', v::string())
|
|
|
|
->key('query', v::notEmpty())
|
|
|
|
)->validate($url);
|
|
|
|
|
|
|
|
It is possible to call methods and closures as the first parameter:
|
|
|
|
|
|
|
|
v::call(array($myObj, 'methodName'), v::int())->validate($myInput);
|
|
|
|
v::call(function($input) {}, v::int())->validate($myInput);
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::callback() - Similar, but a different workflow.
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::callback(callable $callback)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
This is a wildcard validator, it uses a function name, method or closure
|
|
|
|
to validate something:
|
|
|
|
|
|
|
|
v::callback('is_int')->validate(10); //true
|
|
|
|
|
|
|
|
(Please note that this is a sample, the `v::int()` validator is much better).
|
|
|
|
|
|
|
|
As in `v::call()`, you can pass a method or closure to it.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::call() - A more elaborated building block validator
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::cnpj()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates the Brazillian CNPJ number. Ignores non-digit chars, so
|
|
|
|
use `->digits()` if needed.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::cpf() - Validates the Brazillian CPF number.
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::consonants()
|
|
|
|
#### v::consonants(string $additionalChars)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Similar to `v::alnum()`. Validates strings that contain only consonants:
|
|
|
|
|
|
|
|
v::consonants()->validate('xkcd'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::alnum() - a-z0-9, empty or whitespace only
|
|
|
|
* v::digits() - 0-9, empty or whitespace only
|
|
|
|
* v::alpha() - a-Z, empty or whitespace only
|
|
|
|
* v::vowels()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::contains($value)
|
|
|
|
#### v::contains($value, boolean $identical=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
For strings:
|
|
|
|
|
|
|
|
v::contains('ipsum')->validate('lorem ipsum'); //true
|
|
|
|
|
|
|
|
For arrays:
|
|
|
|
|
|
|
|
v::contains('ipsum')->validate(array('ipsum', 'lorem')); //true
|
|
|
|
|
|
|
|
A second parameter may be passed for identical comparison instead
|
|
|
|
of equal comparison.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{containsValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::startsWith()
|
|
|
|
* v::endsWith()
|
|
|
|
* v::in()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::cpf()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates a Brazillian CPF number.
|
|
|
|
|
|
|
|
v::cpf()->validate('44455566820');
|
|
|
|
|
|
|
|
It ignores any non-digit char:
|
|
|
|
|
|
|
|
v::cpf()->validate('444.555.668-20');
|
|
|
|
|
|
|
|
If you need to validate digits only, add `->digits()` to
|
|
|
|
the chain:
|
|
|
|
|
|
|
|
v::digits()->cpf()->validate('44455566820');
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::cnpj()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::creditCard()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates a credit card number.
|
|
|
|
|
|
|
|
v::creditCard()->validate($myCredCardNumber);
|
|
|
|
|
|
|
|
It ignores any non-digit chars, so use `->digits()` when appropriate.
|
|
|
|
|
|
|
|
v::digits()->creditCard()->validate($myCredCardNumber);
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::date()
|
|
|
|
#### v::date($format)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if input is a date:
|
|
|
|
|
|
|
|
v::date()->validate('2009-01-01'); //true
|
|
|
|
|
|
|
|
Also accepts strtotime values:
|
|
|
|
|
|
|
|
v::date()->validate('now'); //true
|
|
|
|
|
|
|
|
And DateTime instances:
|
|
|
|
|
|
|
|
v::date()->validate(new DateTime); //true
|
|
|
|
|
|
|
|
You can pass a format when validating strings:
|
|
|
|
|
|
|
|
v::date('Y-m-d')->validate('01-01-2009'); //false
|
|
|
|
|
|
|
|
Format has no effect when validating DateTime instances.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{format}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::between()
|
|
|
|
* v::minimumAge()
|
|
|
|
* v::leapDate()
|
|
|
|
* v::leapYear()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::digits()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
This is similar to v::alnum(), but it doesn't allow a-Z. It also
|
|
|
|
accepts empty values and whitespace, so use `v::notEmpty()` and
|
|
|
|
`v::noWhitespace()` when appropriate.
|
|
|
|
|
|
|
|
* v::alnum() - a-z0-9, empty or whitespace only
|
|
|
|
* v::alpha() - a-Z, empty or whitespace only
|
|
|
|
* v::vowels()
|
|
|
|
* v::consonants()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::domain()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates domain names.
|
|
|
|
|
|
|
|
v::domain()->validate('google.com');
|
|
|
|
|
|
|
|
This is a composite validator, it validates several rules
|
|
|
|
internally:
|
|
|
|
|
|
|
|
* If input is an IP address, it validates
|
|
|
|
* If input contains whitespace, it fails
|
|
|
|
* If input not contains any dot, it fails
|
|
|
|
* If input has less than two parts, it fails
|
|
|
|
* Input must end with a top-level-domain to pass
|
|
|
|
* Each part must be alphanumeric and not start with an hyphen
|
|
|
|
|
|
|
|
Messages for this validator will reflect rules above.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::tld()
|
|
|
|
* v::ip()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::each(v $validatorForValue)
|
|
|
|
#### v::each(null, v $validatorForKey)
|
|
|
|
#### v::each(v $validatorForValue, v $validatorForKey)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Iterates over an array or Iterator and validates the value or key
|
|
|
|
of each entry:
|
|
|
|
|
|
|
|
$releaseDates = array(
|
|
|
|
'validation' => '2010-01-01',
|
|
|
|
'template' => '2011-01-01',
|
|
|
|
'relational' => '2011-02-05',
|
|
|
|
);
|
|
|
|
|
|
|
|
v::arr()->each(v::date())->validate($releaseDates); //true
|
|
|
|
v::arr()->each(v::date(), v::string()->lowercase())->validate($releaseDates); //true
|
|
|
|
|
|
|
|
Using `arr()` before `each()` is a best practice.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::key()
|
|
|
|
* v::arr()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::email()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates an email address.
|
|
|
|
|
|
|
|
v::email()->validate('alexandre@gaigalas.net'); //true
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::endsWith($value)
|
|
|
|
#### v::endsWith($value, boolean $identical=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
This validator is similar to `v::contains()`, but validates
|
|
|
|
only if the value is at the end of the input.
|
|
|
|
|
|
|
|
For strings:
|
|
|
|
|
|
|
|
v::endsWith('ipsum')->validate('lorem ipsum'); //true
|
|
|
|
|
|
|
|
For arrays:
|
|
|
|
|
|
|
|
v::endsWith('ipsum')->validate(array('lorem', 'ipsum')); //true
|
|
|
|
|
|
|
|
A second parameter may be passed for identical comparison instead
|
|
|
|
of equal comparison.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{endValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::startsWith()
|
|
|
|
* v::contains()
|
|
|
|
* v::in()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::equals($value)
|
|
|
|
#### v::equals($value, boolean $identical=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is equal some value.
|
|
|
|
|
|
|
|
v::equals('alganet')->validate('alganet'); //true
|
|
|
|
|
|
|
|
Identical validation (===) is possible:
|
|
|
|
|
|
|
|
v::equals(10)->validate('10'); //true
|
|
|
|
v::equals(10, true)->validate('10'); //false
|
|
|
|
|
|
|
|
Message template for this validator includes `{{compareTo}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::contains()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::even()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates an even number.
|
|
|
|
|
|
|
|
v::int()->even()->validate(2); //true
|
|
|
|
|
|
|
|
Using `int()` before `even()` is a best practice.
|
|
|
|
|
|
|
|
See also
|
|
|
|
|
|
|
|
* v::odd()
|
|
|
|
* v::multiple()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::float()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates a floating point number.
|
|
|
|
|
|
|
|
v::float()->validate(1.5); //true
|
|
|
|
v::float()->validate('1e5'); //true
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::hexa()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates an hexadecimal number
|
|
|
|
|
|
|
|
v::hexa()->validate('AF12'); //true
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::in($haystack)
|
|
|
|
#### v::in($haystack, boolean $identical=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is contained in a specific haystack.
|
|
|
|
|
|
|
|
For strings:
|
|
|
|
|
|
|
|
v::in('lorem ipsum')->validate('ipsum'); //true
|
|
|
|
|
|
|
|
For arrays:
|
|
|
|
|
|
|
|
v::in(array('lorem', 'ipsum'))->validate('lorem'); //true
|
|
|
|
|
|
|
|
A second parameter may be passed for identical comparison instead
|
|
|
|
of equal comparison.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{haystack}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::startsWith()
|
|
|
|
* v::endsWith()
|
|
|
|
* v::contains()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::instance($instanceName)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is an instance of the given class or interface.
|
|
|
|
|
|
|
|
v::instance('DateTime')->validate(new DateTime); //true
|
|
|
|
v::instance('Traversable')->validate(new ArrayObjet); //true
|
|
|
|
|
|
|
|
Message template for this validator includes `{{instanceName}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::object()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::int()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is an integer.
|
|
|
|
|
|
|
|
v::int()->validate('10'); //true
|
|
|
|
v::int()->validate(10); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::numeric()
|
|
|
|
* v::digits()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::ip()
|
|
|
|
#### v::ip($options)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates IP Addresses. This validator uses the native filter_var()
|
|
|
|
PHP function.
|
|
|
|
|
|
|
|
v::ip()->validate('192.168.0.1');
|
|
|
|
|
|
|
|
You can pass a parameter with filter_var flags for IP.
|
|
|
|
|
|
|
|
v::ip(FILTER_FLAG_NO_PRIV_RANGE)->validate('127.0.0.1'); //false
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::json()
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
Validates if the given input is a valid JSON.
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-09 00:08:39 +02:00
|
|
|
v::json->validate('{"foo":"bar"}'); //true
|
2011-02-07 15:46:08 +01:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::key($name)
|
|
|
|
#### v::key($name, v $validator)
|
|
|
|
#### v::key($name, v $validator, boolean $mandatory=true)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates an array key.
|
|
|
|
|
|
|
|
$dict = array(
|
|
|
|
'foo' => 'bar'
|
2011-02-07 15:46:08 +01:00
|
|
|
);
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
v::key('foo')->validate($dict); //true
|
|
|
|
|
|
|
|
You can also validate the key value itself:
|
|
|
|
|
|
|
|
v::key('foo', v::equals('bar'))->validate($dict); //true
|
|
|
|
|
|
|
|
Third parameter makes the key presence optional:
|
|
|
|
|
|
|
|
v::key('lorem', v::string(), false)->validate($dict); // true
|
|
|
|
|
|
|
|
The name of this validator is automatically set to the key name.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::attribute() - Validates a specific attribute of an object
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::leapDate($format)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if a date is leap.
|
|
|
|
|
|
|
|
v::leapDate('Y-m-d')->validate('1988-02-29'); //true
|
|
|
|
|
|
|
|
This validator accepts DateTime instances as well. The $format
|
|
|
|
parameter is mandatory.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::date()
|
|
|
|
* v::leapYear()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::leapYear()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if a year is leap.
|
|
|
|
|
|
|
|
v::leapYear()->validate('1988'); //true
|
|
|
|
|
|
|
|
This validator accepts DateTime instances as well.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::date()
|
|
|
|
* v::leapDate()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::length($min, $max)
|
|
|
|
#### v::length($min, null)
|
|
|
|
#### v::length(null, $max)
|
|
|
|
#### v::length($min, $max, boolean $inclusive=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates lengths. Most simple example:
|
|
|
|
|
|
|
|
v::string()->length(1, 5)->validate('abc'); //true
|
|
|
|
|
|
|
|
You can also validate only minimum length:
|
|
|
|
|
|
|
|
v::string()->length(5, null)->validate('abcdef'); // true
|
|
|
|
|
|
|
|
Only maximum length:
|
|
|
|
|
|
|
|
v::string()->length(null, 5)->validate('abc'); // true
|
|
|
|
|
|
|
|
The type as the first validator in a chain is a good practice,
|
|
|
|
since length accepts many types:
|
|
|
|
|
|
|
|
v::arr()->length(1, 5)->validate(array('foo', 'bar')); //true
|
|
|
|
|
|
|
|
A third parameter may be passed to validate the passed values inclusive:
|
|
|
|
|
|
|
|
v::string()->length(1, 5, true)->validate('a'); //true
|
|
|
|
|
|
|
|
Message template for this validator includes `{{minValue}}` and `{{maxValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::between() - Validates ranges
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::lowercase()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if string characters are lowercase in the input:
|
|
|
|
|
|
|
|
v::string()->lowercase()->validate('xkcd'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::uppercase()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::macAddress()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates a Mac Address.
|
|
|
|
|
|
|
|
v::macAddress()->validate('00:11:22:33:44:55'); //true
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::max()
|
|
|
|
#### v::max(boolean $inclusive=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input doesn't exceed the maximum value.
|
|
|
|
|
|
|
|
v::int()->max(15)->validate(20); //false
|
|
|
|
|
|
|
|
Also accepts dates:
|
|
|
|
|
|
|
|
v::date()->max('2012-01-01')->validate('2010-01-01'); //true
|
|
|
|
|
|
|
|
`true` may be passed as a parameter to indicate that inclusive
|
|
|
|
values must be used.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{maxValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::min()
|
|
|
|
* v::between()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::min()
|
|
|
|
#### v::min(boolean $inclusive=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input doesn't exceed the minimum value.
|
|
|
|
|
|
|
|
v::int()->min(15)->validate(5); //false
|
|
|
|
|
|
|
|
Also accepts dates:
|
|
|
|
|
|
|
|
v::date()->min('2012-01-01')->validate('2015-01-01'); //true
|
|
|
|
|
|
|
|
`true` may be passed as a parameter to indicate that inclusive
|
|
|
|
values must be used.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{minValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::max()
|
|
|
|
* v::between()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::minimumAge($age)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates a minimum age for a given date.
|
|
|
|
|
|
|
|
v::date()->minimumAge(18)->validate('1987-01-01'); //true
|
|
|
|
|
|
|
|
Using `date()` before is a best-practice.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{age}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::date()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::multiple($multipleOf)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is a multiple of the given parameter
|
|
|
|
|
|
|
|
v::int()->multiple(3)->validate(9); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::primeNumber()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::negative()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if a number is lower than zero
|
|
|
|
|
|
|
|
v::numeric()->negative()->validate(-15); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::positive()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::noWhitespace()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if a string contains no whitespace (spaces, tabs and line breaks);
|
|
|
|
|
|
|
|
v::noWhitespace()->validate('foo bar'); //false
|
|
|
|
|
|
|
|
This is most useful when chaining with other validators such as `v::alnum()`
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::noneOf($v1, $v2, $v3...)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if NONE of the given validators validate:
|
|
|
|
|
|
|
|
v::noneOf(
|
|
|
|
v::int(),
|
|
|
|
v::float()
|
|
|
|
)->validate('foo'); //true
|
|
|
|
|
|
|
|
In the sample above, 'foo' isn't a integer nor a float, so noneOf returns true.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::not()
|
|
|
|
* v::allOf()
|
|
|
|
* v::oneOf()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::not(v $negatedValidator)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Negates any rule.
|
|
|
|
|
|
|
|
v::not(v::ip())->validate('foo'); //true
|
|
|
|
|
|
|
|
In the sample above, validator returns true because 'foo' isn't an IP Address.
|
|
|
|
|
|
|
|
You can negate complex, grouped or chained validators as well:
|
|
|
|
|
|
|
|
v::not(v::int()->positive())->validate(-1.5); //true
|
|
|
|
|
|
|
|
Each other validation has custom messages for negated rules.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::noneOf()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::notEmpty()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the given input is not empty. This function ignores whitespace, so
|
|
|
|
use `noWhitespace()` when appropriate.
|
|
|
|
|
|
|
|
v::string()->notEmpty()->validate(''); //false
|
|
|
|
|
|
|
|
Null values are empty:
|
|
|
|
|
|
|
|
v::notEmpty()->validate(null); //false
|
|
|
|
|
|
|
|
Numbers:
|
|
|
|
|
|
|
|
v::int()->notEmpty()->validate(0); //false
|
|
|
|
|
|
|
|
Empty arrays:
|
|
|
|
|
|
|
|
v::arr()->notEmpty()->validate(array()); //false
|
|
|
|
|
|
|
|
Whitespace:
|
|
|
|
|
|
|
|
v::string()->noWhitespace()->validate(' '); //false
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::noWhitespace()
|
|
|
|
* v::nullValue()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::nullValue()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is null.
|
|
|
|
|
|
|
|
v::nullValue()->validate(null); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::notEmpty()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::numeric()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates on any numeric value.
|
|
|
|
|
|
|
|
v::numeric()->validate(-12); //true
|
|
|
|
v::numeric()->validate('135.0'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::int()
|
|
|
|
* v::digits()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::object()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if the input is an object.
|
|
|
|
|
|
|
|
v::object()->validate(new stdClass); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::instance()
|
|
|
|
* v::attribute()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::odd()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates an odd number.
|
|
|
|
|
|
|
|
v::int()->odd()->validate(3); //true
|
|
|
|
|
|
|
|
Using `int()` before `odd()` is a best practice.
|
|
|
|
|
|
|
|
See also
|
|
|
|
|
|
|
|
* v::even()
|
|
|
|
* v::multiple()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::oneOf($v1, $v2, $v3...)
|
2012-04-09 00:55:05 +02:00
|
|
|
|
|
|
|
This is a group validator that acts as an OR operator.
|
|
|
|
|
|
|
|
v::oneOf(
|
|
|
|
v::int(),
|
|
|
|
v::float()
|
|
|
|
)->validate(15.5); //true
|
|
|
|
|
|
|
|
In the sample above, `v::int()` doesn't validates, but
|
|
|
|
`v::float()` validates, so oneOf returns true.
|
|
|
|
|
|
|
|
`v::oneOf` returns true if at least one inner validator
|
|
|
|
passes.
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
See also:
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::allOf() - Similar to oneOf, but act as an AND operator
|
|
|
|
* v::noneOf() - Validates if NONE of the inner rules validates
|
|
|
|
* v::when() - A ternary validator
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::perfectSquare()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates a perfect square.
|
|
|
|
|
|
|
|
v::perfectSquare()->validate(25); //true (5*5)
|
|
|
|
v::perfectSquare()->validate(9); //true (3*3)
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::positive()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
Validates if a number is higher than zero
|
|
|
|
|
|
|
|
v::numeric()->positive()->validate(-15); //false
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::negative()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::primeNumber()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates a prime number
|
|
|
|
|
|
|
|
v::primeNumber()->validate(7); //true
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::regex($regex)
|
2012-04-09 00:55:05 +02:00
|
|
|
|
|
|
|
Evaluates a regex on the input and validates if matches
|
|
|
|
|
|
|
|
v::regex('/[a-z]/')->validate('a'); //true
|
|
|
|
|
|
|
|
Message template for this validator includes `{{regex}}`
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::roman()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates roman numbers
|
|
|
|
|
|
|
|
v::roman()->validate('IV'); //true
|
|
|
|
|
|
|
|
This validator ignores empty values, use `notEmpty()` when
|
|
|
|
appropriate.
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::sf($sfValidator)
|
2012-04-09 00:55:05 +02:00
|
|
|
|
|
|
|
Use Symfony2 validators inside Respect\Validation flow. Messages
|
|
|
|
are preserved.
|
|
|
|
|
|
|
|
v::sf('Time')->validate('15:00:00');
|
|
|
|
|
|
|
|
You must add Symfony2 to your autoloading routines.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::zend()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::slug()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates slug-like strings:
|
|
|
|
|
|
|
|
v::slug()->validate('my-wordpress-title'); //true
|
|
|
|
v::slug()->validate('my-wordpress--title'); //false
|
|
|
|
v::slug()->validate('my-wordpress-title-'); //false
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::startsWith($value)
|
|
|
|
#### v::startsWith($value, boolean $identical=false)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
This validator is similar to `v::contains()`, but validates
|
|
|
|
only if the value is at the end of the.
|
|
|
|
|
|
|
|
For strings:
|
|
|
|
|
|
|
|
v::startsWith('lorem')->validate('lorem ipsum'); //true
|
|
|
|
|
|
|
|
For arrays:
|
|
|
|
|
|
|
|
v::startsWith('lorem')->validate(array('lorem', 'ipsum')); //true
|
|
|
|
|
|
|
|
`true` may be passed as a parameter to indicate idetical comparison
|
|
|
|
instead of equal.
|
|
|
|
|
|
|
|
Message template for this validator includes `{{startValue}}`.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::endsWith()
|
|
|
|
* v::contains()
|
|
|
|
* v::in()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::string()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates a string.
|
|
|
|
|
|
|
|
v::string()->validate('hi'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::alnum()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::tld()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates a top-level domain
|
|
|
|
|
|
|
|
v::tld()->validate('com'); //true
|
|
|
|
v::tld()->validate('ly'); //true
|
|
|
|
v::tld()->validate('org'); //true
|
|
|
|
|
|
|
|
See also
|
|
|
|
|
|
|
|
* v::domain()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::uppercase()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates if string characters are uppercase in the input:
|
|
|
|
|
|
|
|
v::string()->uppercase()->validate('W3C'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::lowercase()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::version()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Validates version numbers using Semantic Versioning.
|
|
|
|
|
|
|
|
v::version()->validate('1.0.0');
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::vowels()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
Similar to `v::alnum()`. Validates strings that contain only vowels:
|
|
|
|
|
|
|
|
v::vowels()->validate('aei'); //true
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::alnum() - a-z0-9, empty or whitespace only
|
|
|
|
* v::digits() - 0-9, empty or whitespace only
|
|
|
|
* v::alpha() - a-Z, empty or whitespace only
|
|
|
|
* v::consonants()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::when(v $if, v $then, v $else)
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
A ternary validator that accepts three parameters.
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
When the $if validates, returns validation for $then.
|
|
|
|
When the $if doesnt' validate, returns validation for $else.
|
|
|
|
|
|
|
|
v::when(v::int(), v::positive(), v::notEmpty())->validate($input);
|
|
|
|
|
|
|
|
In the sample above, if `$input` is an integer, then it must be positive.
|
|
|
|
If `$input` is not an integer, then it must not me empty.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
* v::allOf()
|
|
|
|
* v::oneOf()
|
|
|
|
* v::noneOf()
|
|
|
|
|
2012-04-10 03:46:28 +02:00
|
|
|
#### v::zend($zendValidator)
|
2012-04-09 00:55:05 +02:00
|
|
|
|
|
|
|
Use Zend validators inside Respect\Validation flow. Messages
|
|
|
|
are preserved.
|
|
|
|
|
|
|
|
v::zend('Hostname')->validate('google.com');
|
|
|
|
|
|
|
|
You need to put Zend Framework in your autoload routines.
|
|
|
|
|
|
|
|
See also:
|
2012-04-09 00:08:39 +02:00
|
|
|
|
2012-04-09 00:55:05 +02:00
|
|
|
* v::sf()
|
2012-04-09 00:08:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|