You are browsing the unmaintained documentation for old Nette 2.3. See documentation for current Nette.

Value Validators

Need to quickly and easily verify that a variable contains for example a valid email address? Then Nette\Utils\Validators will come in handy, a static class with useful functions for validating values.


composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Validators;

Basic Usage

The class Validators has a number of methods for validating values, such as isList(), isUnicode(), isEmail(), isUrl(), etc. for use in your code:

if (!Validators::isEmail($email)) {
	throw new InvalidArgumentException;

Furthermore, it can verify whether the value satisfies the so-called expected types, which is a string where the individual options are separated by a vertical bar |. This makes it easy to verify union types using if():

if (!Validators::is($val, 'int|string|bool')) {

But it also gives you the opportunity to create a system where it is necessary to write expectation as strings (for example in annotations or configuration) and then verify according to them.

You can also declare assertion, which throws an exception if is not met.

Expected Types

An expected types is a string consisting of one or more variants separated by a vertical bar |, similar to writing types in PHP (ie. 'int|string|bool').

Some types can be followed by a colon and the length :length or the range :[min]..[max], eg string:10 (a string with a length of 10 bytes), float:10.. (number 10 and bigger), array:..10 (array of up to ten elements) or list:10..20 (list with 10 to 20 elements), or a regular expression for pattern:[0-9]+.

Overview of types and rules:

PHP types
array range for the number of items can be given
float range for the value can be given
int range for the value can be given
scalar int|float|bool|string
string range for the length in bytes can be given
list indexed array, range for the number of items can be given
none empty value: '', null, false
number int|float
numeric number including textual representation
numericint integer including textual representation
unicode UTF-8 string, range for the length in characters can be given
character class (cannot be an empty string)
alnum all characters are alphanumeric
alpha ​​ all characters are letters [A-Za-z]
digit all characters are digits
lower all characters are lowercase letters [a-z]
space all characters are spaces
upper all characters are uppercase letters [A-Z]
xdigit all characters are hexadecimal digits [0-9A-Fa-f]
syntax validation
pattern a regular expression which the entire string must match
email E-mail
identifier PHP identifier
url URL
uri URI


assert($value, string $expected, string $label=‘variable’)void

Verifies that the value is of expected types separated by pipe. If not, it throws exception Nette\Utils\AssertionException. The word variable in the exception message can be replaced by parameter $label.

Validators::assert('Nette', 'string:5'); // OK
Validators::assert('Lorem ipsum dolor sit', 'string:78');
// AssertionException: The variable expects to be string:78, string 'Lorem ipsum dolor sit' given.

assertField(array $array, string|int $key, string $expected=NULL, string $label=NULL)void

Verifies that element $key in array $array is of expected types separated by pipe. If not, it throws exception Nette\Utils\AssertionException. The string item '%' in array in the exception message can be replaced by parameter $label.

$arr = array('foo' => 'Nette');

Validators::assertField($arr, 'foo', 'string:5'); // OK
Validators::assertField($arr, 'bar', 'string:15');
// AssertionException: Missing item 'bar' in array.
Validators::assertField($arr, 'foo', 'int');
// AssertionException: The item 'foo' in array expects to be int, string 'Nette' given.


is($value, string $expected)bool

Checks if the value is of expected types separated by pipe.

Validators::is(1, 'int|float');  // TRUE
Validators::is(23, 'int:0..10'); // FALSE
Validators::is('Nette Framework', 'string:15');     // TRUE, length is 15 bytes
Validators::is('Nette Framework', 'string:8..');    // TRUE
Validators::is('Nette Framework', 'string:30..40'); // FALSE

isEmail(mixed $value): bool

Checks if the value is a valid email address. It does not verify that the domain actually exists, only the syntax is verified. The function also counts on future TLDs, which may also be in unicode.

Validators::isEmail(''); // TRUE
Validators::isEmail('example@localhost'); // FALSE
Validators::isEmail('nette');             // FALSE

isInRange(mixed $value, array $range)bool

It verifies whether the value is in the given range [min, max], where the upper or lower limit can be omitted (NULL).

Validators::isInRange(5, array(0, 5));     // TRUE
Validators::isInRange(23, array(NULL, 5)); // FALSE
Validators::isInRange(23, array(5));       // TRUE
Validators::isInRange(1, array(5));        // FALSE

isList(mixed $value): bool

Verifies that the value is an indexed array.

Validators::isList(array('a', 'b'));   // TRUE
Validators::isList(array('key' => 'a')); // FALSE
Validators::isList(array(1 => 'a', 0 => 'b')); // FALSE

isNone(mixed $value): bool

Checks if the value is 0, '', false or null.

Validators::isNone(0); // true
Validators::isNone(''); // true
Validators::isNone(false); // true
Validators::isNone(null); // true
Validators::isNone('nette'); // false

isNumeric(mixed $value): bool

Checks if the value is a number or a number written in a string.

Validators::isNumeric(23);      // TRUE
Validators::isNumeric(1.78);    // TRUE
Validators::isNumeric('-42');   // TRUE
Validators::isNumeric('3.14');  // TRUE
Validators::isNumeric('nette'); // FALSE
Validators::isNumeric('1e6');   // FALSE

isNumericInt(mixed $value)bool

Checks if the value is an integer or a integer written in a string.

Validators::isNumericInt(23);      // TRUE
Validators::isNumericInt(1.78);    // FALSE
Validators::isNumericInt('-42');   // TRUE
Validators::isNumericInt('3.14');  // FALSE
Validators::isNumericInt('nette'); // FALSE

isPhpIdentifier(string $value)bool

Checks if the value is a syntactically valid identifier in PHP, for example for names of classes, methods, functions, etc.

Validators::isPhpIdentifier('');        // FALSE
Validators::isPhpIdentifier('Hello1');  // TRUE
Validators::isPhpIdentifier('1Hello');  // FALSE
Validators::isPhpIdentifier('one two'); // FALSE

isUnicode(mixed $value): bool

Checks if the value is a valid UTF-8 string.

Validators::isUnicode('nette'); // TRUE
Validators::isUnicode('');      // TRUE
Validators::isUnicode("\xA0");  // FALSE

isUrl(mixed $value): bool

Checks if the value is a valid URL address.

Validators::isUrl(''); // TRUE
Validators::isUrl('http://localhost');            // TRUE
Validators::isUrl('');          // TRUE
Validators::isUrl('http://[::1]');                // TRUE
Validators::isUrl('');  // FALSE
Validators::isUrl('');                   // FALSE

isUri(string $value): bool

Checks if the value is a valid URI address, that is, actually a string beginning with a syntactically valid schema.

Validators::isUri('');           // TRUE
Validators::isUri('');  // TRUE
Validators::isUrl('');                   // FALSE