respect-validation/docs
Mohammad Amiri 1ce8ac8c7e Update FilterVar.md
Fixed a mistake in Url validation examples
2016-05-05 13:12:45 +02:00
..
Age.md Create "Age" rule 2015-02-19 19:38:16 -02:00
AllOf.md Update documentation 2015-02-12 14:37:33 -02:00
Alnum.md Update documentation 2015-02-12 14:37:33 -02:00
Alpha.md Update documentation 2015-02-12 14:37:33 -02:00
AlwaysInvalid.md Update documentation 2015-02-12 14:37:33 -02:00
AlwaysValid.md Update documentation 2015-02-12 14:37:33 -02:00
Arr.md Update documentation 2015-02-12 14:37:33 -02:00
Attribute.md Update documentation 2015-02-12 14:37:33 -02:00
Bank.md Update documentation 2015-02-12 14:37:33 -02:00
BankAccount.md Update documentation 2015-02-12 14:37:33 -02:00
Between.md Update documentation 2015-02-12 14:37:33 -02:00
Bic.md Update documentation 2015-02-12 14:37:33 -02:00
Bool.md Update documentation 2015-02-12 14:37:33 -02:00
Call.md Update documentation 2015-02-12 14:37:33 -02:00
Callback.md Update documentation 2015-02-12 14:37:33 -02:00
Charset.md Update documentation 2015-02-12 14:37:33 -02:00
Cnh.md Update documentation 2015-02-12 14:37:33 -02:00
Cnpj.md Update documentation 2015-02-12 14:37:33 -02:00
Cntrl.md Update documentation 2015-02-12 14:37:33 -02:00
CONCRETE_API.md Add "CONCREATE_API.md" to repository 2015-02-12 15:07:10 -02:00
Consonant.md Update documentation 2015-02-12 14:37:33 -02:00
Contains.md Update documentation 2015-02-12 14:37:33 -02:00
CountryCode.md Remove "Country" rule 2015-04-24 19:26:49 -03:00
Cpf.md Update documentation 2015-02-12 14:37:33 -02:00
CreditCard.md Update documentation 2015-02-12 14:37:33 -02:00
Date.md Update documentation 2015-02-12 14:37:33 -02:00
Digit.md Update documentation 2015-02-12 14:37:33 -02:00
Directory.md Update documentation 2015-02-12 14:37:33 -02:00
Domain.md Update documentation 2015-02-12 14:37:33 -02:00
Each.md Update documentation 2015-02-12 14:37:33 -02:00
Email.md Update documentation 2015-02-12 14:37:33 -02:00
EndsWith.md Update documentation 2015-02-12 14:37:33 -02:00
Equals.md Update documentation 2015-02-12 14:37:33 -02:00
Even.md Update documentation 2015-02-12 14:37:33 -02:00
Executable.md Update documentation 2015-02-12 14:37:33 -02:00
Exists.md Update documentation 2015-02-12 14:37:33 -02:00
False.md Update documentation 2015-02-12 14:37:33 -02:00
File.md Update documentation 2015-02-12 14:37:33 -02:00
FilterVar.md Update FilterVar.md 2016-05-05 13:12:45 +02:00
Float.md Update documentation 2015-02-12 14:37:33 -02:00
Graph.md Update documentation 2015-02-12 14:37:33 -02:00
HexRgbColor.md Update documentation 2015-02-12 14:37:33 -02:00
In.md Update documentation 2015-02-12 14:37:33 -02:00
INSTALL.md Add PHP and HHVM supported versions on docs 2015-02-26 18:01:48 -03:00
Instance.md Update documentation 2015-02-12 14:37:33 -02:00
Int.md Update documentation 2015-02-12 14:37:33 -02:00
Ip.md Update documentation 2015-02-12 14:37:33 -02:00
Json.md Update documentation 2015-02-12 14:37:33 -02:00
Key.md Update documentation 2015-02-12 14:37:33 -02:00
LeapDate.md Update documentation 2015-02-12 14:37:33 -02:00
LeapYear.md Update documentation 2015-02-12 14:37:33 -02:00
Length.md Update documentation 2015-02-12 14:37:33 -02:00
Lowercase.md Update documentation 2015-02-12 14:37:33 -02:00
MacAddress.md Update documentation 2015-02-12 14:37:33 -02:00
Max.md Update documentation 2015-02-12 14:37:33 -02:00
Min.md Update documentation 2015-02-12 14:37:33 -02:00
MinimumAge.md Create "Age" rule 2015-02-19 19:38:16 -02:00
Multiple.md Update documentation 2015-02-12 14:37:33 -02:00
Negative.md Update documentation 2015-02-12 14:37:33 -02:00
NfeAccessKey.md Update documentation 2015-02-12 14:37:33 -02:00
No.md Update documentation 2015-02-12 14:37:33 -02:00
NoneOf.md Update documentation 2015-02-12 14:37:33 -02:00
Not.md Update documentation 2015-02-12 14:37:33 -02:00
NotEmpty.md Update documentation 2015-02-12 14:37:33 -02:00
NoWhitespace.md Update documentation 2015-02-12 14:37:33 -02:00
NullValue.md Update documentation 2015-02-12 14:37:33 -02:00
Numeric.md Update documentation 2015-02-12 14:37:33 -02:00
Object.md Update documentation 2015-02-12 14:37:33 -02:00
Odd.md Update documentation 2015-02-12 14:37:33 -02:00
OneOf.md Update documentation 2015-02-12 14:37:33 -02:00
PerfectSquare.md Update documentation 2015-02-12 14:37:33 -02:00
Phone.md Update documentation 2015-02-12 14:37:33 -02:00
Positive.md Update documentation 2015-02-12 14:37:33 -02:00
PostalCode.md Update documentation 2015-02-12 14:37:33 -02:00
PrimeNumber.md Update documentation 2015-02-12 14:37:33 -02:00
Prnt.md Update documentation 2015-02-12 14:37:33 -02:00
Punct.md Update documentation 2015-02-12 14:37:33 -02:00
Readable.md Update documentation 2015-02-12 14:37:33 -02:00
README.md Update "See also" section in *.md files 2015-02-21 07:35:02 -02:00
Regex.md Update documentation 2015-02-12 14:37:33 -02:00
Roman.md Update documentation 2015-02-12 14:37:33 -02:00
Sf.md Update documentation 2015-02-12 14:37:33 -02:00
Slug.md Update documentation 2015-02-12 14:37:33 -02:00
Space.md Update documentation 2015-02-12 14:37:33 -02:00
StartsWith.md Update documentation 2015-02-12 14:37:33 -02:00
String.md Update documentation 2015-02-12 14:37:33 -02:00
SymbolicLink.md Update documentation 2015-02-12 14:37:33 -02:00
Tld.md Remove "Country" rule 2015-04-24 19:26:49 -03:00
True.md Update documentation 2015-02-12 14:37:33 -02:00
Type.md Update documentation 2015-02-12 14:37:33 -02:00
Uploaded.md Update documentation 2015-02-12 14:37:33 -02:00
Uppercase.md Update documentation 2015-02-12 14:37:33 -02:00
Url.md Update documentation 2015-02-12 14:37:33 -02:00
VALIDATORS.md Remove "Country" rule 2015-04-24 19:26:49 -03:00
Version.md Update documentation 2015-02-12 14:37:33 -02:00
Vowel.md Update documentation 2015-02-12 14:37:33 -02:00
When.md Update documentation 2015-02-12 14:37:33 -02:00
Writable.md Update documentation 2015-02-12 14:37:33 -02:00
Xdigit.md Update documentation 2015-02-12 14:37:33 -02:00
Yes.md Update documentation 2015-02-12 14:37:33 -02:00
Zend.md Update documentation 2015-02-12 14:37:33 -02:00

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

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

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)

See also: