PHPackages                             gajus/vlad - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. gajus/vlad

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

gajus/vlad
==========

Input validation library, that has inbuilt error messages that are translatable, validators that are easy to extend, and that has easy to understand test declaration syntax.

0.3.3(11y ago)101418[3 issues](https://github.com/gajus/vlad/issues)BSD-3-ClausePHPPHP &gt;=5.4

Since Feb 25Pushed 11y ago5 watchersCompare

[ Source](https://github.com/gajus/vlad)[ Packagist](https://packagist.org/packages/gajus/vlad)[ Docs](https://github.com/gajus/vlad)[ RSS](/packages/gajus-vlad/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (7)Used By (0)

Vlad
====

[](#vlad)

[![Build Status](https://camo.githubusercontent.com/20da04846a6f946d1a55c1c1eca1390601281be6a96c2ade5cb0aef88937e413/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f67616a75732f766c61642e7376673f7374796c653d666c6174)](https://travis-ci.org/gajus/vlad)[![Coverage Status](https://camo.githubusercontent.com/0c4fabfd32031a0263342c220d0d1eb061a56175682e8dcb875455df0d06a8cc/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6a656b796c6c2f6a656b796c6c2e7376673f7374796c653d666c6174)](https://coveralls.io/r/gajus/vlad?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/e0ea5842c84b7ccf9da69e43bf40e9047cb8433b4701eb63d80380bfe0915d1b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67616a75732f766c61642e7376673f7374796c653d666c6174)](https://packagist.org/packages/gajus/vlad)

Input validation library, that has inbuilt error messages that are translatable, validators that are easy to extend, and that has easy to understand test declaration syntax.

Succinct Test Declaration
-------------------------

[](#succinct-test-declaration)

Test is composed of assertions about the input.

```
/**
 * @param Gajus\Vlad\Translator $translator
 */
$test = new \Gajus\Vlad\Test();

/**
 * Add an assertion to the test.
 *
 * @param string $selector_name
 * @return Gajus\Vlad\Assertion
 */
$assertion = $test->assert('user[first_name]');

/**
 * @param string $validator_name
 * @param array $validator_options
 * @param array $condition_options
 * @return Gajus\Vlad\Assertion
 */
$assertion->is('NotEmpty');
$assertion->is('String');
$assertion->is('LengthMin', ['length' => 5]);
$assertion->is('LengthMax', ['length' => 20]);

// In practise, assertion is declared using chaining:
$test
    ->assert('user[last_name]')
    ->is('NotEmpty')
    ->is('String')
    ->is('LengthMin', ['length' => 5])
    ->is('LengthMax', ['length' => 20]);

/**
 * @param array $source
 * @param string $selector_name
 * @return array Errors.
 */
$assessment = $test->assess($_POST);

if ($assessment) {
    // Iterate through error messages.
    foreach ($assessment as $error) {
        // [..]
    }
}
```

### Limit the Assessment Scope

[](#limit-the-assessment-scope)

Note that assertions are done against selector name, not the actual value. You can limit test to specific assertions at the time of the assessment:

```
/**
 * @param string $selector_name
 * @param mixed $value
 * @return string Error.
 */
$error = $test->assertion('user[first_name]', $_POST);
```

Extendable Validation Rules
---------------------------

[](#extendable-validation-rules)

Vlad has [inbuilt validators](https://github.com/gajus/vlad#inbuilt-validation-rules). It is easy to write [custom validators](https://github.com/gajus/vlad#writing-a-custom-validator). You can [request new validators](https://github.com/gajus/vlad/issues) to be added to the core package. Validators benefit from the translator interface.

Vlad does not encourage inline boolean validation expressions.

### Inbuilt Validation Rules

[](#inbuilt-validation-rules)

ValidatorDescription[String](src/Validator/String.php)Validate that input is a string.[Regex](src/Validator/Regex.php)Validate that input is matched using a regular expression.[Date](src/Validator/Date.php)Validates that string can be parsed using a date format.[RangeMinInclusive](src/Validator/RangeMinInclusive.php)Validate that a numeric input is at least of the given size (inclusive).[RangeMinExclusive](src/Validator/RangeMinExclusive.php)Validate that a numeric input is at least of the given size (exclusive).[RangeMaxInclusive](src/Validator/RangeMaxInclusive.php)Validate that a numeric input is at most of the given size (inclusive).[RangeMaxExclusive](src/Validator/RangeMaxExclusive.php)Validate that a numeric input is at most of the given size (exclusive).[NotEmpty](src/Validator/NotEmpty.php)Validate that input value is not empty.[Length](src/Validator/Length.php)Validate that input string representation is of a specific length.[Integer](src/Validator/Integer.php)Validate that input is an integer.[LengthMin](src/Validator/LengthMin.php)Validate that input string representation is not shorter than the specified length.[LengthMax](src/Validator/LengthMax.php)Validate that input string representation is not longer than the specified length.[In](src/Validator/In.php)Validate that input value is in the haystack.[Email](src/Validator/Email.php)Validate that input value is syntactically valid email address.### Writing a Custom Validator

[](#writing-a-custom-validator)

Each validator is a class that extends `Gajus\Vlad\Validator`. Validators that are not part of the Vlad package must be under a namespace.

```
