Skip to content

Latest commit

 

History

History
executable file
·
2130 lines (1431 loc) · 46.7 KB

README.md

File metadata and controls

executable file
·
2130 lines (1431 loc) · 46.7 KB

Respect\Validation

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads License

The most awesome validation engine ever created for PHP.

  • Complex rules made simple: v::numeric()->positive()->between(1, 256)->validate($myNumber).
  • Granularity control for advanced reporting.
  • More than 90 (fully tested) validators.
  • A concrete API for non fluent usage.

Installation

The package is available on Packagist. Autoloading is PSR-4 compatible.

composer require respect/validation

Feature Guide

Namespace Import

Respect\Validation is namespaced, but you can make your life easier by importing a single class into your context:

use Respect\Validation\Validator as v;

Simple Validation

The Hello World validator is something like this:

$number = 123;
v::numeric()->validate($number); //true

Chained Validation

It is possible to use validators in a chain. Sample below validates a string containing numbers and letters, no whitespace and length between 1 and 15.

$usernameValidator = v::alnum()->noWhitespace()->length(1,15);
$usernameValidator->validate('alganet'); //true

Validating Object Attributes

Given this simple object:

$user = new stdClass;
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';

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()

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.

Input optional

All validators treat input as optional and will accept empty string input as valid, unless otherwise stated in the documentation.

We use the v:notEmpty() validator prefixed to disallow empty input and effectively define the field as mandatory as input will be required or validation will fail.

v::string()->notEmpty()->validate(''); //false input required

Negating Rules

You can use the v::not() to negate any rule:

v::not(v::int())->validate(10); //false, input must not be integer

Validator Reuse

Once created, you can reuse your validator anywhere. Remember $usernameValidator?

$usernameValidator->validate('respect');            //true
$usernameValidator->validate('alexandre gaigalas'); //false
$usernameValidator->validate('#$%');                //false

Exception Types

  • Respect\Validation\Exceptions\NestedValidationExceptionInterface:
    • Use when calling assert().
    • Interface has three methods: getFullMessage(), findMessages(), and getMainMessage().
  • Respect\Validation\Exceptions\ValidationExceptionInterface:
    • Use when calling ::check().
    • All Respect\Validation validation exceptions implement this interface.
    • Interface has one method: getMainMessage();
  • Repect\Validation\Exceptions\ExceptionInterface:
    • All Respect\Validation\Exceptions implement this interface.

Informative Exceptions

When something goes wrong, Validation can tell you exactly what's going on. For this, we use the assert() method instead of validate():

use Respect\Validation\Exceptions\NestedValidationExceptionInterface

try {
    $usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
   echo $exception->getFullMessage();
}

The printed message is exactly this, as a text tree:

\-All of the 3 required rules must pass
  |-"really messed up screen#name" must contain only letters (a-z) and digits (0-9)
  |-"really messed up screen#name" must not contain whitespace
  \-"really messed up screen#name" must have a length between 1 and 15

Getting Messages

The text tree is fine, but unusable on a HTML form or something more custom. You can use findMessages() for that:

use Respect\Validation\Exceptions\NestedValidationExceptionInterface

try {
    $usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
    var_dump($exception->findMessages(array('alnum', 'length', 'noWhitespace')));
}

findMessages() returns an array with messages from the requested validators.

Custom Messages

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 = $exception->findMessages(array(
    '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.

Custom Rules

You also can use your own rules:

v::with('My\\Validation\\Rules\\');
v::myRule(); // Try to load "My\Validation\Rules\MyRule" if any

By default with() appends the given prefix, but you can change this behavior in order to overwrite default rules:

v::with('My\\Validation\\Rules\\', true);
v::alnum(); // Try to use "My\Validation\Rules\Alnum" if any

Validator Name

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');

Zend/Symfony Validators

It is also possible to reuse validators from other frameworks if they are installed:

$hostnameValidator = v::zend('Hostname')->assert('google.com');
$timeValidator     = v::sf('Time')->assert('22:00:01');

Validation Methods

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:

use Respect\Validation\Exceptions\ValidationExceptionInterface

try {
    $usernameValidator->check('really messed up screen#name');
} catch(ValidationExceptionInterface $exception) {
    echo $exception->getMainMessage();
}

Message:

"really messed up screen#name" must contain only letters (a-z) and digits (0-9)

Reference

Types

Generics

Comparing Values

Numeric

String

Arrays

Objects

Date and Time

Group Validators

Regional

Files

Banking

Other

Yes/No

Alphabetically

v::allOf(v $v1, v $v2, v $v3...)

Will validate if all inner validators validates.

v::allOf(
    v::int(),
    v::positive()
)->validate(15); //true

This is similar to the chain (which is an allOf already), but its syntax allows you to set custom names for every node:

v::allOf(
    v::int()->setName('Account Number'),
    v::positive()->setName('Higher Than Zero')
)->setName('Positive integer')
 ->validate(15); //true

See also:

v::alnum()

v::alnum(string $additionalChars)

Validates alphanumeric characters from a-Z and 0-9.

v::alnum()->validate('foo 123'); //true

A parameter for extra characters can be used:

v::alnum('-')->validate('foo - 123'); //true

This validator allows whitespace, if you want to remove them add ->noWhitespace() to the chain:

v::alnum()->noWhitespace->validate('foo 123'); //false

By default empty values are allowed, if you want to invalidate them, add ->notEmpty() to the chain:

v::alnum()->notEmpty()->validate(''); //false

You can restrict case using the ->lowercase() and ->uppercase() validators:

v::alnum()->uppercase()->validate('aaa'); //false

Message template for this validator includes {{additionalChars}} as the string of extra chars passed as the parameter.

See also:

v::alpha()

v::alpha(string $additionalChars)

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.

See also:

v::arr()

Validates if the input is an array or traversable object.

v::arr()->validate(array()); //true
v::arr()->validate(new ArrayObject); //true

See also:

  • v::each() - Validates each member of an array
  • v::key() - Validates a specific key of an array

v::alwaysValid()

Always returns true.

v::alwaysValid()->validate($whatever); //true

See also:

v::alwaysInvalid()

Always return false.

v::alwaysInvalid()->validate($whatever); //false

See also:

v::attribute(string $name)

v::attribute(string $name, v $validator)

v::attribute(string $name, v $validator, boolean $mandatory = true)

Validates an object attribute.

$obj = new stdClass;
$obj->foo = 'bar';

v::attribute('foo')->validate($obj); //true

You can also validate the attribute itself:

v::attribute('foo', v::equals('bar'))->validate($obj); //true

Third parameter makes the attribute presence optional:

v::attribute('lorem', v::string(), false)->validate($obj); // true

The name of this validator is automatically set to the attribute name.

See also:

  • v::key() - Validates a specific key of an array

v::bank(string $countryCode)

Validates a bank.

v::bank("de")->validate("70169464"); //true
v::bank("de")->validate("12345"); //false

These country codes are supported:

  • "de" (Germany) - You must add "malkusch/bav": "~1.0" to your require property on composer.json file.

See also

v::bankAccount(string $countryCode, string $bank)

Validates a bank account for a given bank.

v::bankAccount("de", "70169464")->validate("1112"); //true
v::bankAccount("de", "70169464")->validate("1234"); //false

These country codes are supported:

  • "de" (Germany) - You must add "malkusch/bav": "~1.0" to your require property on composer.json file.

See also

v::between(mixed $start, mixed $end)

v::between(mixed $start, mixed $end, boolean $inclusive = false)

Validates ranges. Most simple example:

v::int()->between(10, 20)->validate(15); //true

The type as the first validator in a chain is a good practice, since between accepts many types:

v::string()->between('a', 'f')->validate('c'); //true

Also very powerful with dates:

v::date()->between('2009-01-01', '2013-01-01')->validate('2010-01-01'); //true

Date ranges accept strtotime values:

v::date()->between('yesterday', 'tomorrow')->validate('now'); //true

A third parameter may be passed to validate the passed values inclusive:

v::date()->between(10, 20, true)->validate(20); //true

Message template for this validator includes {{minValue}} and {{maxValue}}.

See also:

v::bic(string $countryCode)

Validates a BIC (Bank Identifier Code) for a given country.

v::bic("de")->validate("VZVDDED1XXX"); //true
v::bic("de")->validate("VZVDDED1"); //true

Theses country codes are supported:

  • "de" (Germany) - You must add "malkusch/bav": "~1.0" to your require property on composer.json file.

See also

v::bool()

Validates if the input is a boolean value:

v::bool()->validate(true); //true
v::bool()->validate(false); //true

v::call(callable $callback)

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(callable $callback)

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::charset(mixed $charset)

Validates if a string is in a specific charset.

v::charset('ASCII')->validate('açúcar'); //false
v::charset('ASCII')->validate('sugar');  //true
v::charset(array('ISO-8859-1', 'EUC-JP'))->validate('日本国'); // true

The array format is a logic OR, not AND.

v::cnpj()

Validates the Brazillian CNPJ number. Ignores non-digit chars, so use ->digit() if needed.

See also:

  • v::cpf() - Validates the Brazillian CPF number.
  • v::cnh() - Validates the Brazillian driver's license.

v::nfeAccessKey(string $accessKey)

Validates the access key of the Brazilian electronic invoice (NFe).

v::nfeAccessKey()->validate('31841136830118868211870485416765268625116906'); //true

v::consonant()

v::consonant(string $additionalChars)

Similar to v::alnum(). Validates strings that contain only consonants:

v::consonant()->validate('xkcd'); //true

See also:

v::contains(mixed $value)

v::contains(mixed $value, boolean $identical = false)

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::cntrl()

v::cntrl(string $additionalChars)

This is similar to v::alnum(), but only accepts control characters:

v::cntrl()->validate("\n\r\t"); //true

See also:

v::countryCode()

Validates an ISO country code like US or BR.

v::countryCode()->validate('BR'); //true

See also:

  • v::tld() - Validates a top level domain

v::cnh()

Validates a Brazillian driver's license.

v::cnh()->validate('02650306461'); //true

See also:

v::cpf()

Validates a Brazillian CPF number.

v::cpf()->validate('44455566820'); //true

It ignores any non-digit char:

v::cpf()->validate('444.555.668-20'); //true

If you need to validate digits only, add ->digit() to the chain:

v::digit()->cpf()->validate('44455566820'); //true

See also:

v::creditCard()

Validates a credit card number.

v::creditCard()->validate('5376 7473 9720 8720'); //true

It ignores any non-digit chars, so use ->digit() when appropriate.

v::digit()->creditCard()->validate('5376747397208720'); //true

v::date()

v::date(string $format)

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::digit()

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.

See also:

v::domain()

v::domain(boolean $tldCheck = true)

Validates domain names.

v::domain()->validate('google.com');

You can skip top level domain (TLD) checks to validate internal domain names:

v::domain(false)->validate('dev.machine.local');

This is a composite validator, it validates several rules internally:

  • If input is an IP address, it fails.
  • If input contains whitespace, it fails.
  • If input does not contain any dots, it fails.
  • If input has less than two parts, it fails.
  • Input must end with a top-level-domain to pass (if not skipped).
  • Each part must be alphanumeric and not start with an hyphen.
  • PunnyCode is accepted for Internationalizing Domain Names in Applications.

Messages for this validator will reflect rules above.

See also:

v::directory()

Validates directories.

v::directory()->validate(__DIR__); //true
v::directory()->validate(__FILE__); //false

This validator will consider SplFileInfo instances, so you can do something like:

v::directory()->validate(new \SplFileInfo($directory));

See also

v::each(v $validatorForValue)

v::each(null, v $validatorForKey)

v::each(v $validatorForValue, v $validatorForKey)

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::email()

Validates an email address.

v::email()->validate('[email protected]'); //true

v::executable()

Validates if a file is an executable.

v::email()->executable('script.sh'); //true

See also

v::exists()

Validates files or directories.

v::exists()->validate(__FILE__); //true
v::exists()->validate(__DIR__); //true

This validator will consider SplFileInfo instances, so you can do something like:

v::exists()->validate(new \SplFileInfo($file));

See also

v::false()

Validates if a value is considered as false.

v::false()->validate(false); //true
v::false()->validate(0); //true
v::false()->validate('0'); //true
v::false()->validate('false'); //true
v::false()->validate('off'); //true
v::false()->validate('no'); //true

See also

v::endsWith(mixed $value)

v::endsWith(mixed $value, boolean $identical = false)

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::equals(mixed $value)

v::equals(mixed $value, boolean $identical = false)

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::even()

Validates an even number.

v::int()->even()->validate(2); //true

Using int() before even() is a best practice.

See also

v::file()

Validates files.

v::file()->validate(__FILE__); //true
v::file()->validate(__DIR__); //false

This validator will consider SplFileInfo instances, so you can do something like:

v::file()->validate(new \SplFileInfo($file));

See also

v::filterVar(int $filter)

v::filterVar(int $filter, mixed $options)

A wrapper for PHP's filter_var() function.

v::filterVar(FILTER_VALIDATE_EMAIL)->validate('[email protected]'); //true
v::filterVar(FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)->validate('http://example.com'); //true

See also

v::float()

Validates a floating point number.

v::float()->validate(1.5); //true
v::float()->validate('1e5'); //true

v::graph()

v::graph(string $additionalChars)

Validates all characters that are graphically represented.

v::graph()->validate('LKM@#$%4;'); //true

See also:

v::hexRgbColor()

Validates a hex RGB color

v::hexRgbColor()->validate('#FFFAAA'); //true
v::hexRgbColor()->validate('123123'); //true
v::hexRgbColor()->validate('FCD'); //true

See also:

v::in(mixed $haystack)

v::in(mixed $haystack, boolean $identical = false)

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::instance(string $instanceName)

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 ArrayObject); //true

Message template for this validator includes {{instanceName}}.

See also:

v::int()

Validates if the input is an integer.

v::int()->validate('10'); //true
v::int()->validate(10); //true

See also:

v::ip()

v::ip(mixed $options)

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

v::json()

Validates if the given input is a valid JSON.

v::json()->validate('{"foo":"bar"}'); //true

v::key(string $name)

v::key(string $name, v $validator)

v::key(string $name, v $validator, boolean $mandatory = true)

Validates an array key.

$dict = array(
    'foo' => 'bar'
);

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::leapDate(string $format)

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::leapYear()

Validates if a year is leap.

v::leapYear()->validate('1988'); //true

This validator accepts DateTime instances as well.

See also:

v::length(int $min, int $max)

v::length(int $min, null)

v::length(null, int $max)

v::length(int $min, int $max, boolean $inclusive = true)

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::lowercase()

Validates if string characters are lowercase in the input:

v::string()->lowercase()->validate('xkcd'); //true

See also:

v::macAddress()

Validates a Mac Address.

v::macAddress()->validate('00:11:22:33:44:55'); //true
v::macAddress()->validate('af-AA-22-33-44-55'); //true

v::max(mixed $maxValue)

v::max(mixed $maxValue, boolean $inclusive = false)

Validates if the input doesn't exceed the maximum value.

v::int()->max(15)->validate(20); //false
v::int()->max(20)->validate(20); //false
v::int()->max(20, true)->validate(20); //true

Also accepts dates:

v::date()->max('2012-01-01')->validate('2010-01-01'); //true

Also date intervals:

// Same of minimum age validation
v::date()->max('-18 years')->validate('1988-09-09'); //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(mixed $minValue)

v::min(mixed $minValue, boolean $inclusive = false)

Validates if the input is greater than the minimum value.

v::int()->min(15)->validate(5); //false
v::int()->min(5)->validate(5); //false
v::int()->min(5, true)->validate(5); //true

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::minimumAge(int $age)

v::minimumAge(int $age, string $format)

Validates a minimum age for a given date.

v::minimumAge(18)->validate('1987-01-01'); //true
v::minimumAge(18, 'd/m/Y')->validate('01/01/1987'); //true

Using date() before is a best-practice.

Message template for this validator includes {{age}}.

See also:

v::multiple(int $multipleOf)

Validates if the input is a multiple of the given parameter

v::int()->multiple(3)->validate(9); //true

See also:

v::negative()

Validates if a number is lower than zero

v::numeric()->negative()->validate(-15); //true

See also:

v::no()

v::no(boolean $locale)

Validates if value is considered as "No".

v::no()->validate('N'); //true
v::no()->validate('Nay'); //true
v::no()->validate('Nix'); //true
v::no()->validate('No'); //true
v::no()->validate('Nope'); //true
v::no()->validate('Not'); //true

This rule is case insensitive.

If $locale is TRUE, uses the value of nl_langinfo() with NOEXPR constant.

See also:

v::noWhitespace()

Validates if a string contains no whitespace (spaces, tabs and line breaks);

v::noWhitespace()->validate('foo bar');  //false
v::noWhitespace()->validate("foo\nbar"); //false

Like other rules the input is still optional.

v::string()->noWhitespace()->validate('');  //true
v::string()->noWhitespace()->validate(' '); //false

This is most useful when chaining with other validators such as v::alnum()

v::noneOf(v $v1, v $v2, v $v3...)

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 $negatedValidator)

Negates any rule.

v::not(v::ip())->validate('foo'); //true

using a shortcut

v::ip()->not()->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

using a shortcut

v::int()->positive()->not()->validate(-1.5); //true

Each other validation has custom messages for negated rules.

See also:

v::notEmpty()

Validates if the given input is not empty or in other words is input mandatory and required. This function also takes whitespace into account, use noWhitespace() if no spaces or linebreaks and other whitespace anywhere in the input is desired.

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()->notEmpty()->validate('        ');  //false
v::string()->notEmpty()->validate("\t \n \r");  //false

See also:

v::nullValue()

Validates if the input is null. This rule does not allow empty strings to avoid ambiguity.

v::nullValue()->validate(null); //true

See also:

v::numeric()

Validates on any numeric value.

v::numeric()->validate(-12); //true
v::numeric()->validate('135.0'); //true

See also:

v::object()

Validates if the input is an object.

v::object()->validate(new stdClass); //true

See also:

v::odd()

Validates an odd number.

v::int()->odd()->validate(3); //true

Using int() before odd() is a best practice.

See also

v::oneOf(v $v1, v $v2, v $v3...)

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.

Using a shortcut

v::int()->addOr(v::float())->validate(15.5); //true

See also:

  • 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

v::perfectSquare()

Validates a perfect square.

v::perfectSquare()->validate(25); //true (5*5)
v::perfectSquare()->validate(9); //true (3*3)

v::phone()

Validates a valid 7, 10, 11 digit phone number (North America, Europe and most Asian and Middle East countries), supporting country and area codes (in dot, space or dashed notations) such as:

(555)555-5555
555 555 5555
+5(555)555.5555
33(1)22 22 22 22
+33(1)22 22 22 22
+33(020)7777 7777
03-6106666

v::positive()

Validates if a number is higher than zero

v::numeric()->positive()->validate(-15); //false

See also:

v::postalCode(string $countryCode)

Validates a postal code according to the given country code.

v::numeric()->postalCode('BR')->validate('02179000'); //true
v::numeric()->postalCode('BR')->validate('02179-000'); //true
v::numeric()->postalCode('US')->validate('02179-000'); //false

Extracted from GeoNames.

See also:

v::primeNumber()

Validates a prime number

v::primeNumber()->validate(7); //true

v::prnt()

v::prnt(string $additionalChars)

Similar to v::graph but accepts whitespace.

v::prnt()->validate('LMKA0$% _123'); //true

See also:

v::punct()

v::punct(string $additionalChars)

Accepts only punctuation characters:

v::punct()->validate('&,.;[]'); //true

See also:

v::readable()

Validates if the given data is a file exists and is readable.

v::readable()->validate('/path/of/a/readable/file'); //true

v::regex(string $regex)

Evaluates a regex on the input and validates if matches

v::regex('/[a-z]/')->validate('a'); //true

Message template for this validator includes {{regex}}

v::roman()

Validates roman numbers

v::roman()->validate('IV'); //true

This validator ignores empty values, use notEmpty() when appropriate.

v::sf(string $validator)

Use Symfony2 validators inside Respect\Validation flow. Messages are preserved.

v::sf('Time')->validate('15:00:00');

You must add "symfony/validator": "~2.6" to your require property on composer.json file.

See also:

v::slug()

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

v::space()

v::space(string $additionalChars)

Accepts only whitespace:

v::space()->validate('    '); //true

See also:

v::startsWith(mixed $value)

v::startsWith(mixed $value, boolean $identical = false)

This validator is similar to v::contains(), but validates only if the value is at the beginning of the input.

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 identical comparison instead of equal.

Message template for this validator includes {{startValue}}.

See also:

v::string()

Validates a string.

v::string()->validate('hi'); //true

See also:

v::symbolicLink()

Validates if the given data is a path of a valid symbolic link.

v::symbolicLink()->validate('/path/of/valid/symbolic/link'); //true

v::tld()

Validates a top-level domain

v::tld()->validate('com'); //true
v::tld()->validate('ly'); //true
v::tld()->validate('org'); //true

See also

v::true()

Validates if a value is considered as true.

v::true()->validate(true); //true
v::true()->validate(1); //true
v::true()->validate('1'); //true
v::true()->validate('true'); //true
v::true()->validate('on'); //true
v::true()->validate('yes'); //true

See also

v::type(string $type)

Validates the type of input.

v::type('bool')->validate(true); //true
v::type('callable')->validate(function (){}); //true
v::type('object')->validate(new stdClass()); //true

See also

v::uploaded()

Validates if the given data is a file that was uploaded via HTTP POST.

v::uploaded()->validate('/path/of/an/uploaded/file'); //true

v::uppercase()

Validates if string characters are uppercase in the input:

v::string()->uppercase()->validate('W3C'); //true

See also:

v::url()

Validates if input is an URL:

v::url()->validate('http://example.com'); //true
v::url()->validate('https://www.youtube.com/watch?v=6FOUqQt3Kg0'); //true
v::url()->validate('ldap://[::1]'); //true
v::url()->validate('mailto:[email protected]'); //true
v::url()->validate('news:new.example.com'); //true

This rule uses v::filterVar() rule with FILTER_VALIDATE_URL flag.

See also:

v::version()

Validates version numbers using Semantic Versioning.

v::version()->validate('1.0.0');

v::vowel()

Similar to v::alnum(). Validates strings that contains only vowels:

v::vowel()->validate('aei'); //true

See also:

v::when(v $if, v $then, v $else)

v::when(v $if, v $then)

A ternary validator that accepts three parameters.

When the $if validates, returns validation for $then. When the $if doesn't validate, returns validation for $else, if defined.

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. When $else is not defined use v::alwaysInvalid() as default.

See also:

v::xdigit()

Accepts an hexadecimal number:

v::xdigit()->validate('abc123'); //true

Notice, however, that it doesn't accept strings starting with 0x:

v::xdigit()->validate('0x1f'); //false

See also:

v::yes()

v::yes(boolean $locale)

Validates if value is considered as "Yes".

v::yes()->validate('Y');//true
v::yes()->validate('Yea');//true
v::yes()->validate('Yeah');//true
v::yes()->validate('Yep');//true
v::yes()->validate('Yes');//true

This rule is case insensitive.

If $locale is TRUE, uses the value of nl_langinfo() with YESEXPR constant.

See also:

v::writable()

Validates if the given input is writable file.

v::writable()->validate('/path/of/a/writable/file'); //true

v::zend(mixed $validator)

Use Zend validators inside Respect\Validation flow. Messages are preserved.

v::zend('Hostname')->validate('google.com');

You must add "zendframework/zend-validator": "~2.3" to your require property on composer.json file.

See also: