Value Validators
Nette\Utils\Validators is a static class with useful functions for data validation.
All examples assume an alias is created:
use Nette\Utils\Validators;
Rules
Rule definitions are written in a similar way as Latte helpers. Each rule is separated
with vertical bar (|
) and optionally suffixed with parameters after 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 |
|
url |
URL |
none |
“empty value” |
Special | |
pattern |
… |
alpha |
… |
digit |
… |
lower |
… |
upper |
… |
space |
… |
xdigit |
… |
is($value, $expected)
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, $expected, $label =
'variable'
)
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($arr, $field, $expected = NULL, $label =
"item '%' in array"
)
Verifies that the array item corresponds to the rules, otherwise throws an assertion exception.
$arr = array('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($value)
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($value)
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($value)
Verifies that the input is callable.
Validators::isCallable('Foo::bar'); // true
Validators::isCallable(array($foo, 'bar')); // true
Validators::isCallable(array('Foo::bar')); // true
Validators::isCallable('Lorem ipsum dolor sit...'); // false
Validators::isCallable(23); // false
Validators::isCallable(FALSE); // false
isUnicode($value)
Verifies that the input is a valid UTF-8 string.
Validators::isUnicode('nette'); // true
Validators::isUnicode(''); // true
Validators::isUnicode(1); // false
isNone($value)
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($value)
Verifies that the input is non-associative array.
Validators::isList(array('foo', 'bar')); // true
Validators::isList(array('foo' => 'Bar')); // false
isInRange($value, array $range)
Verifies that the input is in a given range.
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
isEmail($value)
Verifies that the input is a valid email address.
Validators::isEmail('example@nette.org'); // true
Validators::isEmail('nette'); // false
isUrl($value)
Verifies that the input is a valid URL address.
Validators::isUrl('https://nette.org'); // true
Validators::isUrl('nette'); // false