Below is a list with all predefined validation rules.
Checks if the data under validation is accepted. By default, the field under validation must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance.
$validator->field('fieldName')->accepted();
You may also pass an array with values which are considered accepted:
$validator->field('fieldName')->accepted(['accept', 'agree', 'ok']);
The field under validation must be "yes", "on", "1", "true", 1
, or true
if another field's value is equal to a given value.
use KrisKuiper\Validator\Validator;
$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);
$validator->field('fieldName')->acceptedIf('other_field', 'foo');
You may also provide the values which should be considered as accepted:
$validator
->field('fieldName')
->acceptedIf('other_field', 'foo', ['accepted', 'agreed', 'checked']);
See also the Accepted and Accepted not empty rules.
The field under validation must be "yes", "on", "1", "true", 1
, or true
if another field's value is not empty.
use KrisKuiper\Validator\Validator;
$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);
$validator->field('fieldName')->acceptedNotEmpty('other_field');
You may also provide the values which should be considered as accepted:
$validator
->field('fieldName')
->acceptedNotEmpty('other_field', ['accepted', 'agreed', 'checked']);
See also the Accepted and Accepted if rules.
Checks if the data under validation is an active URL.
$validator->field('fieldName')->activeURL();
Checks if the data under validation comes after a given date.
$validator->field('fieldName')->after('2000-01-01', 'Y-m-d');
See also the Before, Before or equal and After or equal rules.
Checks if the data under validation comes after or is equal to a given date.
$validator->field('fieldName')->afterOrEqual('2000-01-01', 'Y-m-d');
See also the After, Before or equal and Before rules.
Checks if the field under validation only contains alpha characters (a-z and A-Z).
$validator->field('fieldName')->alpha();
Checks if the field under validation only contains letters (case-insensitive) and numbers, dashes and underscores.
$validator->field('fieldName')->alphaDash();
Checks if the field under validation only exists off letters (case-insensitive) and numbers.
$validator->field('fieldName')->alphaNumeric();
Checks if the field under validation only exists off letters (case-insensitive) and spaces.
$validator->field('fieldName')->alphaSpace();
Checks if the data under validation comes before a given date.
$validator->field('fieldName')->before('2030-01-01', 'Y-m-d');
See also the After, Before or equal and After or equal rules.
Checks if the data under validation comes before or is equal to a given date.
$validator->field('fieldName')->beforeOrEqual('2030-01-01', 'Y-m-d');
See also the After, After or equal and Before rules.
Checks if the data under validation (number) is between a given minimum and maximum.
$validator->field('fieldName')->between(5, 10.5);
Checks if the field under validation has a matching field of {field}_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
$validator->field('fieldName')->confirmed();
Checks if the data under validation contains a given value.
$validator->field('fieldName')->contains('abc');
Use the second parameter to search case-sensitive:
$validator->field('fieldName')->contains('ABC', true);
See also the Contains not rule.
Checks if the data under validation does not contain a given value.
$validator->field('fieldName')->containsNot('ABC');
Use the second parameter to search case-sensitive:
$validator->field('fieldName')->containsNot('ABC', true);
See also the Contains rule.
Checks if the data under validation has at least one letter.
$validator->field('fieldName')->containsLetter();
You can also provide the minimum amount of letters that is required:
$validator->field('fieldName')->containsLetter(3);
Checks if the data under validation has at least one uppercase and one lowercase letter.
$validator->field('fieldName')->containsMixedCase();
You can also provide the minimum amount of lowercase and uppercase letters that is required:
$validator->field('fieldName')->containsMixedCase(3, 4);
Checks if the data under validation has at least one digit.
$validator->field('fieldName')->containsDigit();
You can also provide the minimum amount of digits that is required:
$validator->field('fieldName')->containsDigit(2);
Checks if the data under validation has at least one symbol.
$validator->field('fieldName')->containsSymbol();
You can also provide the minimum amount of symbols that is required:
$validator->field('fieldName')->containsSymbol(2);
Checks if the data under validation contains a given amount of items.
$validator->field('fieldName')->count(10);
Checks if the data under validation is countable.
$validator->field('fieldName')->countable();
Checks if the data under validation contains an amount of items between a given minimum and maximum.
$validator->field('fieldName')->countBetween(5, 10);
Checks if the data under validation contains no more items than a given maximum amount.
$validator->field('fieldName')->countMax(10);
Checks if the data under validation contains at least a given amount of items.
$validator->field('fieldName')->countMin(5);
Checks if the data under validation is a valid CSS color. By default, a valid CSS color may or may not begin with a hash sign (#) and may consist of 6 or 3 hexadecimal characters.
$validator->field('fieldName')->cssColor();
You can define if the hash sign (#) is required or not (#ffffff is valid but ffffff is not):
$validator->field('fieldName')->cssColor(true);
You can also define that shortcodes are not accepted (i.e. #ffffff instead of #fff):
$validator->field('fieldName')->cssColor(true, false);
Checks if the data under validation is a valid date.
$validator->field('fieldName')->date('Y-m-d');
Checks if the data under validation comes between a provided from and to date.
$validator->field('fieldName')->dateBetween('2000-01-01', '2010-01-01', 'Y-m-d');
Checks if the data under validation is declined. By default, the field under validation must be "no", "off", "0", "false", 0
, or false
.
$validator->field('fieldName')->declined();
You may also pass an array with values which are considered declined:
$validator->field('fieldName')->declined(['decline', 'disagree']);
The field under validation must be "no", "off", "0", "false", 0
, or false
if another field's value is equal to a given value.
use KrisKuiper\Validator\Validator;
$data = ['field' => 'no', 'other_field' => 'foo'];
$validator = new Validator($data);
$validator->field('fieldName')->declinedIf('other_field', 'foo');
You may also provide the values which should be considered as declined:
$validator
->field('fieldName')
->declinedIf('other_field', 'foo', ['declined', 'disagree']);
See also the Declined and Declined not empty rules.
The field under validation must be "no", "off", "0", "false", 0
, or false
if another field's value is not empty.
use KrisKuiper\Validator\Validator;
$data = ['field' => 'yes', 'other_field' => 'foo'];
$validator = new Validator($data);
$validator->field('fieldName')->declinedNotEmpty('other_field');
You may also provide the values which should be considered as declined:
$validator
->field('fieldName')
->declinedNotEmpty('other_field', ['declined', 'agreed', 'checked']);
See also the Declined and Declined if rules.
Check if the data under validation does not match one of the values of one or more fields.
$validator->field('fieldName')->different('otherFieldName', 'anotherFieldName');
Check if the data under validation does not match all the values of one or more fields.
$validator->field('fieldName')->differentWithAll('otherFieldName', 'anotherFieldName');
Check if an integer value have exact length of provided digits.
$validator->field('fieldName')->digits(5);
Check if an integer value is between the provided min and max length of digits.
$validator->field('fieldName')->digitsBetween(4, 6);
Check if an integer value has a maximum length of digits.
$validator->field('fieldName')->digitsMax(5);
Check if an integer value has at least the provided length of digits.
$validator->field('fieldName')->digitsMin(5);
Check if all the values in an array are unique.
$validator->field('fieldName')->distinct();
Check if the field under validation is divisible by a provided number
$validator->field('fieldName')->divisible(10);
You can use the strict parameter to strictly check if a value is divisible
$validator->field('fieldName')->divisible(10, true);
Checks if the data under validation is a valid email address.
$validator->field('fieldName')->email();
Checks if the data under validation does not end with a given value.
$validator->field('fieldName')->endsNotWith('abc');
Use the second parameter to match case-sensitive:
$validator->field('fieldName')->endsNotWith('ABC', true);
See also the Ends with, Starts with and Starts not with rules.
Checks if the data under validation ends with a given value.
$validator->field('fieldName')->endsWith('abc');
Use the second parameter to match case-sensitive:
$validator->field('fieldName')->endsWith('ABC', true);
See also the Ends not with, Starts with and Starts not with rules.
Checks if the data under validation equals a provided value.
$validator->field('fieldName')->equals('abc');
Use the second parameter to match case-sensitive:
$validator->field('fieldName')->equals('ABC', true);
Checks if the data under validation is a valid hexadecimal string
$validator->field('fieldName')->hexadecimal();
Checks if the data under validation exists in a given array.
$validator->field('fieldName')->in(['foo', 'bar']);
Use the second parameter to search type safe:
$validator->field('fieldName')->in(['123', 123], true);
See also the Not in rule.
Checks if the data under validation is a valid IP address (v4 or v6).
$validator->field('fieldName')->ip();
Checks if the data under validation is a private IP address (v4 or v6).
$validator->field('fieldName')->ipPrivate();
Checks if the data under validation is a public ip address (v4 or v6).
$validator->field('fieldName')->ipPublic();
Checks if the data under validation is a valid IP v4 address.
$validator->field('fieldName')->ipv4();
Checks if the data under validation is a valid IP v6 address.
$validator->field('fieldName')->ipv6();
Checks if the data under validation is an array.
$validator->field('fieldName')->isArray();
Checks if the data under validation is a boolean true
or false
.
$validator->field('fieldName')->isBool();
Checks if the data under validation is empty. Empty string, empty array and null are considered empty.
$validator->field('fieldName')->isEmpty();
Checks if the data under validation is boolean false
.
$validator->field('fieldName')->isFalse();
Checks if the data under validation is an integer number.
$validator->field('fieldName')->isInt();
You can use the strict parameter to strictly check if a value is an integer number:
$validator->field('fieldName')->isInt(true);
Checks if the data under validation is not null
.
$validator->field('fieldName')->isNotNull();
See also the Is null rule.
Checks if the data under validation is null
.
$validator->field('fieldName')->isNull();
See also the Is not null rule.
Checks if the data under validation is of the type string.
$validator->field('fieldName')->isString();
Checks if value equals boolean true
.
$validator->field('fieldName')->isTrue();
Checks if the data under validation is valid JSON.
$validator->field('fieldName')->json();
Checks if the value character length is the given length.
$validator->field('fieldName')->length(10);
Checks if the data under validation is a valid URL.
$validator->field('fieldName')->lengthBetween(10, 20);
Checks if the amount of characters is less or equal than the given amount.
$validator->field('fieldName')->lengthMax(10);
Checks if the amount of characters is at least a given amount.
$validator->field('fieldName')->lengthMin(5);
Checks if the data under validation is a string containing only lowercase characters.
$validator->field('fieldName')->lowercase();
Checks if the data under validation is a valid MAC address. By default, the dash "-" symbol is used as the delimiter for a valid MAC address.
$validator->field('fieldName')->macAddress();
You can change the delimiter i.e. colon:
$validator->field('fieldName')->macAddress(':');
Checks if the value is less than the given maximum amount.
$validator->field('fieldName')->max(10.5);
Checks if the field under validation is at least a given minimum.
$validator->field('fieldName')->min(10.5;
Checks if the data under validation exists in a given array.
$validator->field('fieldName')->notIn(['foo', 'bar']);
Use the second parameter to search type safe:
$validator->field('fieldName')->notIn(['123', 123], true);
Check if the field under validation is a negative number
$validator->field('fieldName')->negative();
You can use the strict parameter to strictly check if a value is negative
$validator->field('fieldName')->negative(true);
Check if the field under validation is a negative number or is equals to zero.
$validator->field('fieldName')->negativeOrZero();
You can use the strict parameter to strictly check if a value is negative or equals to zero.
$validator->field('fieldName')->negativeOrZero(true);
The field under validation may be null. If the value is null
, then all other rules are bailed (will not be executed).
$validator->field('fieldName')->nullable();
You can also define that the field under validation may not be null
:
$validator->field('fieldName')->nullable(false);
Checks if the data under validation is an integer number.
$validator->field('fieldName')->number();
You can use the strict parameter to strictly check if a value is a number:
$validator->field('fieldName')->number(true);
Check if the field under validation is a positive number.
$validator->field('fieldName')->positive();
You can use the strict parameter to strictly check if a value is positive.
$validator->field('fieldName')->positive(true);
Check if the field under validation is a positive number or is equals to zero.
$validator->field('fieldName')->positiveOrZero();
You can use the strict parameter to strictly check if a value is positive or equals to zero.
$validator->field('fieldName')->positiveOrZero(true);
Check if the data under validation exists as key.
$validator->field('fieldName')->present();
Checks if the field under validation is an empty string or not present.
$validator->field('fieldName')->prohibited();
Checks if the field under validation is considered empty (null, empty string or empty array) or not present if another field is equal to any value.
$validator->field('fieldName')->prohibitedIf('otherFieldName');
Checks if the field under validation is present. If so, the provided field name(s) may not be present, even if empty.
$validator->field('fieldName')->prohibits('otherFieldName');
Check if value matches a regular expression pattern.
$validator->field('fieldName')->regex('/[a-z]+/');
Adds a new rule that will require the field/value (null, '', [] are considered empty).
$validator->field('fieldName')->required();
Checks if the field under validation is an array and contains at least the specified keys.
$validator->field('fieldName')->requiredArrayKeys('foo', 'bar');
The field under validation must be present and not empty only if one of the other specified fields are present or empty.
$validator->field('fieldName')->requiredWith('otherFieldName', 'anotherFieldName');
The field under validation must be present and not empty only if all the other specified fields are present or empty.
$validator->field('fieldName')->requiredWithAll('otherFieldName', 'anotherFieldName');
The field under validation must be present and not empty only if one of the other specified fields are not present or empty.
$validator->field('fieldName')->requiredWithout('otherFieldName', 'anotherFieldName');
The field under validation must be present and not empty only if all the other specified fields are not present or empty.
$validator->field('fieldName')->requiredWithoutAll('otherFieldName', 'anotherFieldName');
Check if value matches a value of a given field name.
$validator->field('password')->same('password-repeat');
Check if value does not match a value of a given field name.
$validator->field('field')->sameNot('other-field');
Checks if the data under validation is a scalar type.
$validator->field('fieldName')->scalar();
Checks if the data under validation does not begin with a given value.
$validator->field('fieldName')->startsNotWith('abc');
Use the second parameter to search case-sensitive:
$validator->field('fieldName')->startsNotWith('ABC', true);
See also the Starts with, Ends with and Ends not with rules.
Checks if the data under validation begins with a given value.
$validator->field('fieldName')->startsWith('abc');
Use the second parameter to search case-sensitive:
$validator->field('fieldName')->startsWith('ABC', true);
See also the Ends with, Ends not with and Starts not with rules.
Checks if the data under validation is a valid time zone.
$validator->field('fieldName')->timezone();
Use the first parameter to search case-insensitive:
$validator->field('fieldName')->timezone(true);
Note: see timezones on php.net for more information.
Checks if the data under validation is a string containing only uppercase characters.
$validator->field('fieldName')->uppercase();
Checks if the data under validation is a valid URL. By default, the protocol will not be checked.
$validator->field('fieldName')->url();
Force the protocol (i.e. http or https):
$validator->field('fieldName')->url(true);
Checks if the data under validation is a valid UUID v1, v3, v4 or v5 entity.
$validator->field('fieldName')->uuid();
Checks if the data under validation is a valid UUID v1 entity.
$validator->field('fieldName')->uuidv1();
Checks if the data under validation is a valid UUID v3 entity.
$validator->field('fieldName')->uuidv3();
Checks if the data under validation is a valid UUID v4 entity.
$validator->field('fieldName')->uuidv4();
Checks if the data under validation is a valid UUID v4 entity.
$validator->field('fieldName')->uuidv5();
Checks if the amount of words is at least to a given amount. By default, a word is defined to have two or more alphanumeric characters.
$validator->field('fieldName')->words(10);
The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:
$validator->field('fieldName')->words(10, 5, false);
See also the Words max and Words min rules.
Checks if the amount of words is less than or equal to a given amount. By default, a word is defined to have two or more alphanumeric characters.
$validator->field('fieldName')->wordsMax(10);
The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:
$validator->field('fieldName')->wordsMax(10, 5, false);
Checks if the amount of words is more than or equal to a given amount. By default, a word is defined to have two or more alphanumeric characters.
$validator->field('fieldName')->wordsMin(10);
The second parameter defines the minimum length of the word, while the third parameter can be used to allow all symbols instead of only alphanumeric characters:
$validator->field('fieldName')->wordsMin(10, 5, false);
Go to the previous section.
Go to the next section.