Data Validation

Nette\Utils\Validators is a static class with useful functions for data validation.

Installation:

composer require nette/utils

All examples assume an alias is created:

use Nette\Utils\Validators;

Rules

Rule definitions are written similarly as Latte filters. Each rule is separated with a vertical line (|) and optionally suffixed with parameters after a colon.

Type
bool, boolean boolean value
int, integer integer
float float
number integer or float
numeric number, including text representation
numericint integer, including text representation
string string
unicode UTF-8 string
array array
list non-associative array
object object
resource resource
scalar scalar (int, float, bool or string)
callable callback
null null
Value
email E-mail
url URL
none “empty value”
Special
pattern
alpha
digit
lower
upper
space
xdigit

is($value, string $expected): bool

Verifies that the input corresponds to the rules.

Validators::is('Nette Framework', 'string:15'); // 15 characters long string
Validators::is('Nette Framework', 'string:8..'); // minimum of 8 characters long string
Validators::is('Nette Framework', 'string:..32'); // maximum of 32 characters long string
Validators::is('Nette Framework', 'string:8..32'); // minimum of 8 and maximum of 32 characters long string
Validators::is(23, 'int:0..10'); // integer between 0 and 10
Validators::is(1, 'int|float'); // integer or float (can be written as `number`)

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

Verifies that the input corresponds to the rules, otherwise throws an assertion exception.

Validators::assert('Nette Framework', 'string:15'); // ok
Validators::assert('Lorem ipsum dolor sit', 'string:78'); // exception
// The variable expects to be string:78, string 'Lorem ipsum dolor sit' given.

assertField(array $arr, string|int $field, string $expected=null, string $label=“item ‘%’ in array”): void

Verifies that the array item corresponds to the rules, otherwise throws an assertion exception.

$arr = ['nette' => 'framework'];

Validators::assertField($arr, 'nette', 'string:15'); // ok
Validators::assertField($arr, 'foo', 'string:15'); // exception
// Missing item 'foo' in array.

Validators::assertField($arr, 'nette', 'int'); // exception
// The item 'nette' in array expects to be int, string 'framework' given.

Validators

isNumericInt(mixed $value): bool

Verifies that the input is an integer.

Validators::isNumericInt(23); // true
Validators::isNumericInt(1.78); // false
Validators::isNumericInt('42'); // true
Validators::isNumericInt('3.14'); // false
Validators::isNumericInt('nette'); // false

isNumeric(mixed $value): bool

Verifies that the input is a number.

Validators::isNumeric(23); // true
Validators::isNumeric(1.78); // true
Validators::isNumeric('42'); // true
Validators::isNumeric('3.14'); // true
Validators::isNumeric('nette'); // false

isCallable(mixed $value): bool

Verifies that the input is callable.

Validators::isCallable('Foo::bar'); // true
Validators::isCallable([$foo, 'bar']); // true
Validators::isCallable(['Foo::bar']); // true
Validators::isCallable('Lorem ipsum dolor sit...'); // false
Validators::isCallable(23); // false
Validators::isCallable(false); // false

isUnicode(mixed $value): bool

Verifies that the input is a valid UTF-8 string.

Validators::isUnicode('nette'); // true
Validators::isUnicode(''); // true
Validators::isUnicode(1); // false

isNone(mixed $value): bool

Verifies that the input is ((none))“empty”.

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

isList(mixed $value): bool

Verifies that the input is non-associative array.

Validators::isList(['foo', 'bar']); // true
Validators::isList(['foo' => 'Bar']); // false

isInRange(mixed $value, array $range): bool

Verifies that the input is in a given range.

Validators::isInRange(5, [0, 5]); // true
Validators::isInRange(23, [null, 5]); // false
Validators::isInRange(23, [5]); // true
Validators::isInRange(1, [5]); // false

isEmail(mixed $value): bool

Verifies that the input is a valid email address.

Validators::isEmail('example@nette.org'); // true
Validators::isEmail('nette'); // false

isUrl(mixed $value): bool

Verifies that the input is a valid URL address.

Validators::isUrl('https://nette.org'); // true
Validators::isUrl('nette'); // false