Ebooks

PHP Respect Validation tutorial

PHP Respect Validation tutorial shows how to validate PHP values with Respect Validation package.

Respect Validation

Respect Validation is a PHP Standalone Validation Library.

Installation

$ composer require respect/validation
$ composer require tightenco/collect

We install the Respect Validation package and the Laravel's collection package.

Respect Validation simple example

In the first example, we show how to do a very simple validation.

simple.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;

$name = "John Doe";

$r = v::alnum()->validate($name);

if ($r) {

    echo "Validation passed";
} else {

    echo "Validation failed";
}

The example validates one value.

use Respect\Validation\Validator as v;

We include the validator.

$r = v::alnum()->validate($name);

The alnum is a validation rule that allows alphanumeric values. It also allows a space. The validate() method validates the rule and returns a boolean value indicating failure or success.

if ($r) {

    echo "Validation passed";
} else {

    echo "Validation failed";
}

Based on the returned value, we show the validation message.

$ php simple.php
Validation passed

This is the output.

Adding a character

We can add a character to a rule.

add_character.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;

$name = "Vane-Tempest-Stewart";

$r = v::alnum('-')->validate($name);

if ($r) {

    echo "Validation passed";
} else {

    echo "Validation failed";
}

In the example, we add a dash to the alnum rule.

$name = "Vane-Tempest-Stewart";

Some names might contain dash characters.

$r = v::alnum('-')->validate($name);

We add a dash character to the alnum rule.

Respect Validation chaining rules

The rules can be chained.

chaining.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;

$name = "John";

$r = v::alnum()->length(4, null)->validate($name);

if ($r) {

    echo "Validation passed";
} else {

    echo "Validation failed";
}

In the example, we have two rules: alnum and length.

$r = v::alnum()->length(4, null)->validate($name);

The name must contain only alphanumeric characters and must have at least four characters.

Respect Validation combining rules

The combining of rules is similar to the chaining technique. The Rules\AllOf is used to combine multiple rules.

combine_rules.php
<?php
require('vendor/autoload.php');

use Respect\Validation\Validator as v;
use Respect\Validation\Rules;

$name = "John";

$nameValidator = new Rules\AllOf(
    new Rules\Alnum(),
    new Rules\Length(5, 40)
);

$r = $nameValidator->validate($name);

if ($r) {

    echo "Validation passed";
} else {

    echo "Validation failed";
}

The example combines two validation rules with Rules\AllOf.

Respect validation assert function

The validate() method returns a boolean value. The assert() method returns a complete validation report.

assert_fun.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationException;

$name = "";

$validator = v::alnum()->notempty();

try {

    $validator->assert($name);
} catch(NestedValidationException $ex) {

    $coll = collect($ex->getMessages());

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        echo $message . "\n";
    }
}

The example uses an assert() method on a validation rule and shows the error messages.

$validator->assert($name);

We validate a name value with assert().

$coll = collect($ex->getMessages());

$messages = $coll->flatten();

foreach ($messages as $message) {
    echo $message . "\n";
}

With the help of the Laravel's collections, we show the error messages.

$ php assert_fun.php
"" must contain only letters (a-z) and digits (0-9)
"" must not be empty

This is the output.

Respect Validation between rule

The between rule validates ranges including integers, characters, and dates.

between_rule.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;

$age = 34; 

$r = v::intVal()->between(18, 99)->validate($age); 

if ($r) {

    echo "Age validation passed\n";
} else {

    echo "Age validation failed\n";
}

$char = 'g'; 

$r = v::stringType()->between('a', 'c')->validate($char); 

if ($r) {

    echo "Letter validation passed\n";
} else {

    echo "Letter validation failed\n";
}

$myDate = '2013-01-01';

$r = v::date()->between('2009-01-01', '2019-01-01')->validate($myDate);

if ($r) {

    echo "Date validation passed\n";
} else {

    echo "Date validation failed\n";
}

The example uses the between rule.

$r = v::intVal()->between(18, 99)->validate($age); 

This rule checks if the $age variable is between 18 and 99.

$r = v::stringType()->between('a', 'c')->validate($char); 

This rule checks if the $char variable is between 'a' and 'c' characters.

$r = v::date()->between('2009-01-01', '2019-01-01')->validate($myDate);

This rule checks if the $myDate variable is between two specified dates.

Respect Validation objects

Object attributes are validated with attribute().

user.php
<?php

class User {

    private $name;
    private $email;

    public function getName() : string {

        return $this->name;
    }

    public function setName($name) : void {

        $this->name = $name;
    }

    public function getEmail() : string {

        return $this->email;
    }

    public function setEmail($email) : void {

        $this->email = $email;
    }
}

This is user.php.

validate_object.php
<?php

require('vendor/autoload.php');
require_once('user.php');

use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationException;

$user = new User();
$user->setName('Jo');
$user->setEmail('johndoe#gmail.com');

$userValidator = v::attribute('name', v::alnum()->length(4, null))
    ->attribute('email', v::email());

try {
    $userValidator->assert($user);
} catch(NestedValidationException $ex) {

    $coll = collect($ex->getMessages());

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        echo $message . "\n";
    }
}

The example validates the attributes of the User class.

$userValidator = v::attribute('name', v::alnum()->length(4, null))
    ->attribute('email', v::email());

These are rules for the attributes of the user object.

$ php validate_object.php
name must have a length greater than 4
email must be valid email

This is the output.

Custom message

We can provide custom validation messages.

custom_message.php
<?php

require('vendor/autoload.php');

use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationException;

$name = "";

$validator = v::alnum()->notEmpty()->setName('name');

try {

    $validator->assert($name);
} catch(NestedValidationException $ex) {

    $errors = $ex->findMessages([
        'alnum' => '{{name}} must contain only letters and digits',
        'notEmpty' => '{{name}} must not be empty'
    ]);

    $coll = collect($errors);

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        echo $message . "\n";
    }
}

The example adds two custom messages.

$validator = v::alnum()->notEmpty()->setName('name');

With the setName() method, we set a value for a template placeholder.

$errors = $ex->findMessages([
    'alnum' => '{{name}} must contain only letters and digits',
    'notEmpty' => '{{name}} must not be empty'
]);

Here we provide custom error messages.

$ php custom_message.php
name must contain only letters and digits
name must not be empty

This is the output.

You might also be interested in the following related tutorials: PHP Rakit Validation tutorial, Valitron tutorial, PHP PDO tutorial, Twig tutorial, PHP filesystem functions, PHP tutorial.

In this tutorial, we have used PHP Respect Validation library to validate values.