Respect/Validation PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Respect/Validation for PHP: Complete Guide with Examples

Respect/Validation is a powerful and elegant PHP library for data validation. Unlike built-in PHP functions like filter_var or regular expressions, Respect/Validation offers an object-oriented, fluent interface for building complex validation rules. The library lets you create readable, self-documenting checks that are easy to maintain and extend.

Why do you need this library? Every web application must validate user input: emails, passwords, URLs, IP addresses, dates, numeric ranges, and much more. Without a validation system, your code quickly devolves into a mess of if-else statements and regex patterns. Respect/Validation solves this by providing over 100 ready-made rules (validators) that you can combine into powerful validation chains. The library also supports custom error messages in multiple languages, making it ideal for international projects.

The key feature is validation chains. You can chain multiple rules together, and they execute sequentially. If any rule fails, validation stops (by default) and returns an error. This lets you write compact yet highly expressive code.

Installation

Install via Composer, the standard PHP package manager. Run this in your project root:

composer require respect/validation

After installation, include the Composer autoloader in your PHP file:

require_once 'vendor/autoload.php';

Quick Start

A minimal working example for validating an email and password:

use Respect\Validation\Validator as v;

$email = 'user@example.com';
$password = 'StrongP@ss1';

// Create a validation chain
$validator = v::email()->length(6, 100)->notEmpty();

if ($validator->validate($email)) {
    echo "Email is valid!";
} else {
    // Get error messages
    $errors = $validator->getMessages();
    print_r($errors);
}

// Password validation
$passwordValidator = v::alnum()->noWhitespace()->length(8, 32);
if ($passwordValidator->validate($password)) {
    echo "Password is valid!";
}

In this example, v::email() checks the email format, length(6, 100) checks string length, and notEmpty() ensures the value is not empty. All rules are chained together.

Core Methods and Classes

Let's explore 8 key methods (rules) of the library. Each rule is a static method of the Respect\Validation\Validator class (typically imported as v).

1. v::email()

Signature: public static function email(): Validator

What it does: Checks if the value is a valid email address according to RFC 5321/5322.

Example:

$validator = v::email();
var_dump($validator->validate('test@test.com')); // true
var_dump($validator->validate('not-an-email')); // false

2. v::alnum()

Signature: public static function alnum(string $additionalChars = ''): Validator

What it does: Checks if the string contains only letters (a-z, A-Z) and digits (0-9). You can pass additional allowed characters.

Example:

$validator = v::alnum();
var_dump($validator->validate('abc123')); // true
var_dump($validator->validate('hello world')); // false (space not allowed)

// With additional characters
$validatorWithDash = v::alnum('-');
var_dump($validatorWithDash->validate('my-name')); // true

Recommendations