4.7.4. Rules To Validate Fields

4.7.4.1. alnum

Validates the value as alphanumeric only.

$filter->validate('field')->is('alnum');

4.7.4.2. alpha

Validates the value as alphabetic only.

$filter->validate('field')->is('alpha');

4.7.4.3. between

Validates the value as being within or equal to a minimum and maximum value.

$filter->validate('field')->is('between', $min, $max);

4.7.4.4. blank

Validates the value as being blank.

$filter->validate('field1')->isBlank();
$filter->validate('field2')->isBlankOr('strlen', 3);
$filter->validate('field3')->isBlankOrNot('strlen', 3);

To validate the value as not being blank.

$filter->validate('field')->isNotBlank();

4.7.4.5. bool

Validates the value as being a boolean, or a pseudo-boolean. Pseudo-true values include the strings '1', 'y', 'yes', and 'true'; pseudo-false values include the strings '0', 'n', 'no', and 'false'.

$filter->validate('field')->is('bool');

4.7.4.6. callback

Validates the value using a callable/callback. The callable should take two arguments, $subject and $field, to indicate the subject and the field within that subject. It should return true to pass, or false to fail.

$filter->validate('field')->is('callback', function ($subject, $field) {
    if ($subject->$field === 'foo') {
        return true;
    }
    return false;
});

N.b.: Always use object notation ($subject->$field) and not array notation ($subject[$field]) in the closure, as the Filter converts arrays to objects on the fly.

4.7.4.7. creditCard

Validates the value as being a credit card number.

$filter->validate('field')->is('creditCard');

4.7.4.8. dateTime

Validates the value as representing a date and/or time.

$filter->validate('field')->is('dateTime');

4.7.4.9. email

Validates the value as being a properly-formed email address per the various relevant RFCs. If the intl extension is loaded, it will also allow for international domain names.

$filter->validate('field')->is('email');

4.7.4.10. equalToField

Validates the value as loosely equal (==) to the value of another field in the subject.

$filter->validate('field')->is('equalToField', 'other_field_name');

4.7.4.11. equalToValue

Validates the value as loosely equal (`==') to a specified value.

$filter->validate('field')->is('equalToValue', $other_value);

4.7.4.12. float

Validates the value as representing a float.

$filter->validate('field')->is('float');

4.7.4.13. inKeys

Validates that the value is loosely equal (==) to a key in a given array.

$filter->validate('field')->is('inKeys', $array);

4.7.4.14. int

Validates the value as representing an integer.

$filter->validate('field')->is('int');

4.7.4.15. inValues

Validates that the value is strictly equal (===) to a value in a given array.

$filter->validate('field')->is('inValues', $array);

4.7.4.16. ip

Validates the value as an IPv4 or IPv6 address, allowing reserved and private addresses.

$filter->validate('field')->is('ip');

To modify restrictions on the filter, pass the appropriate FILTER_FLAG_* constants (seen here) as a second parameter.

// only allow IPv4 addresses in the non-private range.
$filter->validate('field')->is('ip', FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE);

// only allow IPv6 addresses in non-reserved range.
$filter->validate('field')->is('ip', FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE);

4.7.4.17. isbn

Validates the value is a correct ISBN (International Standard Book Number).

$filter->validate('field')->is('isbn');

4.7.4.18. locale

Validates the given value against a list of locale strings (internal to the rule class).

$filter->validate('field')->is('locale');

4.7.4.19. lowercase

Validates the value as all lowercase.

$filter->validate('field')->is('lowercase');

4.7.4.20. lowercaseFirst

Validates the value begins with a lowercase character.

$filter->validate('field')->is('lowercaseFirst');

4.7.4.21. max

Validates the value as being less than or equal to a maximum.

$filter->validate('field')->is('max', $max);

4.7.4.22. min

Validates the value as being greater than or equal to a minimum.

$filter->validate('field')->is('min', $min);

4.7.4.23. regex

Validates the value using preg_match().

$filter->validate('field')->is('regex', $expr);

4.7.4.24. strictEqualToField

Validates the value as strictly equal (===) to the value of another field in the subject.

$filter->validate('field')->is('strictEqualToField', 'other_field_name');

4.7.4.25. strictEqualToValue

Validates the value as strictly equal (===) to a specified value.

$filter->validate('field')->is('strictEqualToValue', $other_value);

4.7.4.26. string

Validates the value can be represented by a string.

$filter->validate('field')->is('string');

4.7.4.27. strlen

Validates the value has a specified length.

$filter->validate('field')->is('strlen', $len);

4.7.4.28. strlenBetween

Validates the value as being within or equal to a minimum and maximum length.

$filter->validate('field')->is('strlenBetween', $min, $max);

4.7.4.29. strlenMax

Validates the value length as being no longer than a maximum.

$filter->validate('field')->is('strlenMax', $max);

4.7.4.30. strlenMin

Validates the value length as being no shorter than a minimum.

$filter->validate('field')->is('strlenMin', $min);

4.7.4.31. titlecase

Validates the value as title case

$filter->validate('field')->is('titlecase');

4.7.4.32. trim

Validates the value is trim()med. Optionally specify characters to trim.

$filter->validate('field')->is('trim', $chars);

4.7.4.33. upload

Validates the value represents a PHP upload information array, and that the file is an uploaded file.

$filter->validate('field')->is('upload');

4.7.4.34. uppercase

Validates the value as all uppercase.

$filter->validate('field')->is('uppercase');

4.7.4.35. uppercaseFirst

Validates the value begins with an uppercase character.

$filter->validate('field')->is('uppercaseFirst');

4.7.4.36. url

Validates the value is a well-formed URL.

$filter->validate('field')->is('url');

4.7.4.37. word

Validates the value as being composed only of word characters.

$filter->validate('field')->is('word');