Rules uses ConstantsTrait
The available rules constants.
Tags
Table of Contents
Constants
- ACCEPTED = 'accepted'
- The field under this rule must be one of 'on', 'yes', '1', 'true' (the string "true"), or true.
- AFTER = 'after'
- The field under this rule must be a date after the given minimum.
- ALPHA = 'alpha'
- The field under this rule must be entirely alphabetic characters.
- ALPHA_DASH = 'alpha_dash'
- The field under this rule may have alpha-numeric characters, as well as dashes and underscores.
- ALPHA_NUM = 'alpha_num'
- The field under this rule must be entirely alpha-numeric characters.
- ALPHA_SPACES = 'alpha_spaces'
- The field under this rule may have alpha characters, as well as spaces.
- ANY_OF = 'any'
- A variation of in: here the values (separated by default with a ,) must all be in the given values.
- ARRAY = 'array'
- The field under this rule must be an array.
- ARRAY_CAN_ONLY_HAVE_KEYS = 'array_can_only_have_keys'
- The array can only contain the specified keys, any keys not present will fail validation.
- ARRAY_MUST_HAVE_KEYS = 'array_must_have_keys'
- The array must contain all the specified keys to be valid.
- BEFORE = 'before'
- The field under this rule must be a date before the given maximum.
- BETWEEN = 'between'
- The field under this rule must have a size between min and max params. Value size is calculated in the same way as min and max rule.
- BOOLEAN = 'boolean'
- The field under this rule must be boolean. Accepted inputs are true, false, 1, 0, "1", and "0".
- CALLBACK = 'callback'
- Define a custom callback to validate the value. This rule cannot be registered using the string syntax.
- COLOR = 'color'
- Ensures that a given value matches a valid color expression (e.g. "#ff0000").
- DATE = 'date'
- The field under this rule must be valid date following a given format.
- DEFAULT = 'default'
- If the attribute has no value, this default will be used in place in the validated data.
- DIFFERENT = 'different'
- Opposite of same; the field value under this rule must be different to another_field value.
- DIGITS = 'digits'
- The field under validation must be numeric and must have an exact length of value.
- DIGITS_BETWEEN = 'digits_between'
- The field under validation must be numeric and have a length between the given min and max.
- ELEVATION = 'elevation'
- Ensures that a value represents a valid elevation (altitude) in meters.
- EMAIL = 'email'
- The field under this validation must be a valid email address according to the built-in PHP filter extension.
- ENDS_WITH = 'ends_with'
- The field under this validation must end with another_field.
- EQUAL = 'equal'
- Ensures a given value is **equal to** another field's value or to a fixed numeric constant.
- EXTENSION = 'extension'
- The field under this rule must end with an extension corresponding to one of those listed.
- FLOAT = 'float'
- The field under validation must be an float.
- GREATER_THAN = 'gt'
- Ensures a given value is **greater than** another field's value or to a fixed numeric constant.
- GREATER_THAN_OR_EQUAL = 'gte'
- Ensures a given value is **greater than or equal to** another field's value or to a fixed numeric constant.
- IN = 'in'
- The field under this rule must be included in the given list of values.
- INTEGER = 'integer'
- The field under validation must be an integer.
- IP = 'ipv'
- The field under this rule must be a valid ipv4 or ipv6 address.
- IPV4 = 'ipv4'
- The field under this rule must be a valid ipv4 address.
- IPV6 = 'ipv6'
- The field under this rule must be a valid ipv6 address.
- JSON = 'json'
- The field under this validation must be a valid JSON string.
- LATITUDE = 'latitude'
- Validates that a value represents a valid geographic latitude.
- LENGTH = 'length'
- The field under this validation must be a string of exactly the length specified.
- LESS_THAN = 'lt'
- Ensures a given value is **less than** another field's value or to a fixed numeric constant.
- LESS_THAN_OR_EQUAL = 'lte'
- Ensures a given value is **less than or equal to** another field's value or to a fixed numeric constant.
- LONGITUDE = 'longitude'
- Validates that a value represents a valid geographic longitude.
- LOWERCASE = 'lowercase'
- The field under this validation must be in lowercase.
- MAX = 'max'
- The field under this rule must have a size less than or equal to the given number. Value size is calculated in the same way as the min rule.
- MIMES = 'mimes'
- The $_FILES item under validation must have a MIME type corresponding to one of the listed extensions.
- MIN = 'min'
- The field under this rule must have a size greater than or equal to the given number.
- NOT_IN = 'not_in'
- The field under this rule must not be included in the given list of values.
- NULLABLE = 'nullable'
- The field under this rule may be empty.
- PRESENT = 'present'
- The field under this rule must be in the set of inputs, whatever the value is.
- PROHIBITED = 'prohibited'
- The field under this rule is not allowed.
- PROHIBITED_IF = 'prohibited_if'
- The field under this rule is not allowed if another_field is provided with any of the value(s).
- PROHIBITED_UNLESS = 'prohibited_unless'
- The field under this rule is not allowed unless another_field has one of these values.
- RANGE = 'range'
- Validates that a numeric value lies between a minimum and a maximum value (inclusive).
- REGEX = 'regex'
- The field under this rule must match the given regex.
- REJECTED = 'rejected'
- The field under this rule must have a value that corresponds to rejection i.e. 0 (zero), "0", false, no, "false", off.
- REQUIRED = 'required'
- The field under this validation must be present and contain a non-empty value.
- REQUIRED_IF = 'required_if'
- The field under this rule must be present and not empty if the another_field field is equal to any value.
- REQUIRED_UNLESS = 'required_unless'
- The field under validation must be present and not empty unless the another_field field is equal to any value.
- REQUIRED_WITH = 'required_with'
- The field under validation must be present and not empty only if any of the other specified fields are present.
- REQUIRED_WITH_ALL = 'required_with_all'
- The field under validation must be present and not empty only if all the other specified fields are present.
- REQUIRED_WITHOUT = 'required_without'
- The field under validation must be present and not empty only when any of the other specified fields are not present.
- REQUIRED_WITHOUT_ALL = 'required_without_all'
- The field under validation must be present and not empty only when all the other specified fields are not present.
- REQUIRES = 'requires'
- The field under validation requires that the specified fields are present in the input data and are not empty.
- SAME = 'same'
- The field value under this rule must have the same value as another_field.
- SOMETIMES = 'sometimes'
- Sometimes attributes can be left off or can be null.
- STARTS_WITH = 'starts_with'
- The field under this validation must start with another_field.
- STRING = 'string'
- The field under this rule must be a PHP string.
- UPPERCASE = 'uppercase'
- The field under this validation must be in uppercase.
- URL = 'url'
- The field under this rule must be a valid url format.
- UUID = 'uuid'
- The field under this validation must be a valid UUID and not the nil UUID string.
Constants
ACCEPTED
The field under this rule must be one of 'on', 'yes', '1', 'true' (the string "true"), or true.
public
mixed
ACCEPTED
= 'accepted'
AFTER
The field under this rule must be a date after the given minimum.
public
mixed
AFTER
= 'after'
The parameter should be any valid string that can be parsed by strtotime. For example:
Tags
ALPHA
The field under this rule must be entirely alphabetic characters.
public
mixed
ALPHA
= 'alpha'
ALPHA_DASH
The field under this rule may have alpha-numeric characters, as well as dashes and underscores.
public
mixed
ALPHA_DASH
= 'alpha_dash'
ALPHA_NUM
The field under this rule must be entirely alpha-numeric characters.
public
mixed
ALPHA_NUM
= 'alpha_num'
ALPHA_SPACES
The field under this rule may have alpha characters, as well as spaces.
public
mixed
ALPHA_SPACES
= 'alpha_spaces'
ANY_OF
A variation of in: here the values (separated by default with a ,) must all be in the given values.
public
mixed
ANY_OF
= 'any'
ARRAY
The field under this rule must be an array.
public
mixed
ARRAY
= 'array'
ARRAY_CAN_ONLY_HAVE_KEYS
The array can only contain the specified keys, any keys not present will fail validation.
public
mixed
ARRAY_CAN_ONLY_HAVE_KEYS
= 'array_can_only_have_keys'
By default, associative data has no restrictions on the key => values that can be present.
For example: you have filters for a search box that are passed to SQL, only the specified keys should be allowed to be sent and not any value in the array of filters.
Tags
ARRAY_MUST_HAVE_KEYS
The array must contain all the specified keys to be valid.
public
mixed
ARRAY_MUST_HAVE_KEYS
= 'array_must_have_keys'
This is useful to ensure that a nested array meets a prescribed format. The same thing can be achieved by using individual rules for each key with required.
Note that this will still allow additional keys to be present, it merely validates the presence of specific keys.
This rule is best used in conjunction with the array rule, though it can be used standalone.
Tags
BEFORE
The field under this rule must be a date before the given maximum.
public
mixed
BEFORE
= 'before'
This also works the same way as the after rule. Pass anything that can be parsed by strtotime
Tags
BETWEEN
The field under this rule must have a size between min and max params. Value size is calculated in the same way as min and max rule.
public
mixed
BETWEEN
= 'between'
You can also validate the size of uploaded files using this rule:
Tags
BOOLEAN
The field under this rule must be boolean. Accepted inputs are true, false, 1, 0, "1", and "0".
public
mixed
BOOLEAN
= 'boolean'
CALLBACK
Define a custom callback to validate the value. This rule cannot be registered using the string syntax.
public
mixed
CALLBACK
= 'callback'
To use this rule, you must use the array syntax and either explicitly specify callback, or pass the closure:
Tags
COLOR
Ensures that a given value matches a valid color expression (e.g. "#ff0000").
public
mixed
COLOR
= 'color'
Tags
DATE
The field under this rule must be valid date following a given format.
public
mixed
DATE
= 'date'
Parameter format is optional, default format is Y-m-d.
DEFAULT
If the attribute has no value, this default will be used in place in the validated data.
public
mixed
DEFAULT
= 'default'
Tags
DIFFERENT
Opposite of same; the field value under this rule must be different to another_field value.
public
mixed
DIFFERENT
= 'different'
Tags
DIGITS
The field under validation must be numeric and must have an exact length of value.
public
mixed
DIGITS
= 'digits'
Tags
DIGITS_BETWEEN
The field under validation must be numeric and have a length between the given min and max.
public
mixed
DIGITS_BETWEEN
= 'digits_between'
Tags
ELEVATION
Ensures that a value represents a valid elevation (altitude) in meters.
public
mixed
ELEVATION
= 'elevation'
Tags
The field under this validation must be a valid email address according to the built-in PHP filter extension.
public
mixed
EMAIL
= 'email'
See FILTER_VALIDATE_EMAIL for details.
ENDS_WITH
The field under this validation must end with another_field.
public
mixed
ENDS_WITH
= 'ends_with'
Comparison can be against strings, numbers and array elements.
EQUAL
Ensures a given value is **equal to** another field's value or to a fixed numeric constant.
public
mixed
EQUAL
= 'equal'
Tags
EXTENSION
The field under this rule must end with an extension corresponding to one of those listed.
public
mixed
EXTENSION
= 'extension'
This is useful for validating a file type for a given path or url. The mimes rule should be used for validating uploads.
FLOAT
The field under validation must be an float.
public
mixed
FLOAT
= 'float'
GREATER_THAN
Ensures a given value is **greater than** another field's value or to a fixed numeric constant.
public
mixed
GREATER_THAN
= 'gt'
Tags
GREATER_THAN_OR_EQUAL
Ensures a given value is **greater than or equal to** another field's value or to a fixed numeric constant.
public
mixed
GREATER_THAN_OR_EQUAL
= 'gte'
Tags
IN
The field under this rule must be included in the given list of values.
public
mixed
IN
= 'in'
To help build the string rule, the In (and NotIn) rules have a helper method:
use Somnambulist\Components\Validation\Factory;
use Somnambulist\Components\Validation\Rules\In;
$factory = new Factory();
$validation = $factory->validate($data,
[
'enabled' =>
[
'required',
In::make([true, 1])
]
]);
This rule uses in_array to perform the validation and by default does not perform strict checking. If you require strict checking, you can invoke the rule like this:
use Somnambulist\Components\Validation\Factory;
$factory = new Factory();
$validation = $factory->validate($data,
[
'enabled' =>
[
'required',
$factory->rule('in')->values([true, 1])->strict()
]
]);
INTEGER
The field under validation must be an integer.
public
mixed
INTEGER
= 'integer'
IP
The field under this rule must be a valid ipv4 or ipv6 address.
public
mixed
IP
= 'ipv'
IPV4
The field under this rule must be a valid ipv4 address.
public
mixed
IPV4
= 'ipv4'
IPV6
The field under this rule must be a valid ipv6 address.
public
mixed
IPV6
= 'ipv6'
JSON
The field under this validation must be a valid JSON string.
public
mixed
JSON
= 'json'
LATITUDE
Validates that a value represents a valid geographic latitude.
public
mixed
LATITUDE
= 'latitude'
Tags
LENGTH
The field under this validation must be a string of exactly the length specified.
public
mixed
LENGTH
= 'length'
LESS_THAN
Ensures a given value is **less than** another field's value or to a fixed numeric constant.
public
mixed
LESS_THAN
= 'lt'
Tags
LESS_THAN_OR_EQUAL
Ensures a given value is **less than or equal to** another field's value or to a fixed numeric constant.
public
mixed
LESS_THAN_OR_EQUAL
= 'lte'
Tags
LONGITUDE
Validates that a value represents a valid geographic longitude.
public
mixed
LONGITUDE
= 'longitude'
Tags
LOWERCASE
The field under this validation must be in lowercase.
public
mixed
LOWERCASE
= 'lowercase'
MAX
The field under this rule must have a size less than or equal to the given number. Value size is calculated in the same way as the min rule.
public
mixed
MAX
= 'max'
You can also validate the maximum size of uploaded files using this rule:
Tags
MIMES
The $_FILES item under validation must have a MIME type corresponding to one of the listed extensions.
public
mixed
MIMES
= 'mimes'
MIN
The field under this rule must have a size greater than or equal to the given number.
public
mixed
MIN
= 'min'
For string values, the size corresponds to the number of characters. For integer or float values, size corresponds to its numerical value. For an array, size corresponds to the count of the array.
If your value is numeric string, you can use the numeric rule to treat its size as a numeric value instead of the number of characters.
You can also validate the minimum size of uploaded files using this rule:
Tags
NOT_IN
The field under this rule must not be included in the given list of values.
public
mixed
NOT_IN
= 'not_in'
This rule also uses in_array and can have strict checks enabled the same way as In.
not_in:value_1,value_2,...
NULLABLE
The field under this rule may be empty.
public
mixed
NULLABLE
= 'nullable'
PRESENT
The field under this rule must be in the set of inputs, whatever the value is.
public
mixed
PRESENT
= 'present'
PROHIBITED
The field under this rule is not allowed.
public
mixed
PROHIBITED
= 'prohibited'
PROHIBITED_IF
The field under this rule is not allowed if another_field is provided with any of the value(s).
public
mixed
PROHIBITED_IF
= 'prohibited_if'
PROHIBITED_UNLESS
The field under this rule is not allowed unless another_field has one of these values.
public
mixed
PROHIBITED_UNLESS
= 'prohibited_unless'
This is the inverse of prohibited_if.
RANGE
Validates that a numeric value lies between a minimum and a maximum value (inclusive).
public
mixed
RANGE
= 'range'
Tags
REGEX
The field under this rule must match the given regex.
public
mixed
REGEX
= 'regex'
Note: if you require the use of |, then the regex rule must be written in array format instead of as a string.
For example:
use Somnambulist\Components\Validation\Factory;
$validation = (new Factory())->validate
([
'field' => 'value'
],
[
'field' =>
[
'required',
'regex' => '/(this|that|value)/'
]
])
REJECTED
The field under this rule must have a value that corresponds to rejection i.e. 0 (zero), "0", false, no, "false", off.
public
mixed
REJECTED
= 'rejected'
This is the inverse of the accepted rule.
REQUIRED
The field under this validation must be present and contain a non-empty value.
public
mixed
REQUIRED
= 'required'
Valid values include non-empty strings, numbers, and arrays with elements. Empty values such as null, empty arrays, or empty strings are considered invalid.
Examples:
| Value | Valid |
|---|---|
| 'something' | true |
| '0' | true |
| 0 | true |
| [0] | true |
| [null] | true |
| null | false |
| [] | false |
| '' | false |
REQUIRED_IF
The field under this rule must be present and not empty if the another_field field is equal to any value.
public
mixed
REQUIRED_IF
= 'required_if'
For example required_if:something,1,yes,on will be required if something's value is one of 1, '1', 'yes', or 'on'.
required_if:another_field,value_1,value_2,...
REQUIRED_UNLESS
The field under validation must be present and not empty unless the another_field field is equal to any value.
public
mixed
REQUIRED_UNLESS
= 'required_unless'
required_unless:another_field,value_1,value_2,...
REQUIRED_WITH
The field under validation must be present and not empty only if any of the other specified fields are present.
public
mixed
REQUIRED_WITH
= 'required_with'
required_with:field_1,field_2,...
Note: the behaviour of this rule can be circumvented if the rule this is defined on is sometimes or nullable.
For example: if a is "required_with:b", but a is also only sometimes present, then the required_with will never trigger as the sometimes rule will negate it. a would also need to be explicitly passed to trigger the rule.
REQUIRED_WITH_ALL
The field under validation must be present and not empty only if all the other specified fields are present.
public
mixed
REQUIRED_WITH_ALL
= 'required_with_all'
required_with_all:field_1,field_2,...
REQUIRED_WITHOUT
The field under validation must be present and not empty only when any of the other specified fields are not present.
public
mixed
REQUIRED_WITHOUT
= 'required_without'
REQUIRED_WITHOUT_ALL
The field under validation must be present and not empty only when all the other specified fields are not present.
public
mixed
REQUIRED_WITHOUT_ALL
= 'required_without_all'
REQUIRES
The field under validation requires that the specified fields are present in the input data and are not empty.
public
mixed
REQUIRES
= 'requires'
requires:field_1,field_2,...
For example: field b "requires:a"; if a is either not present, or has an "empty" value, then the validation fails. "empty" is false, empty string, or null.
This is an extension of required_with, however the rule will fail when used with sometimes or nullable. For example: if b "requires:a" and "a" is allowed to be nullable, b will fail as it explicitly requires a with a value.
SAME
The field value under this rule must have the same value as another_field.
public
mixed
SAME
= 'same'
SOMETIMES
Sometimes attributes can be left off or can be null.
public
mixed
SOMETIMES
= 'sometimes'
These cases should be handled carefully and have different results after validation.
Tags
STARTS_WITH
The field under this validation must start with another_field.
public
mixed
STARTS_WITH
= 'starts_with'
Comparison can be against strings, numbers and array elements.
STRING
The field under this rule must be a PHP string.
public
mixed
STRING
= 'string'
UPPERCASE
The field under this validation must be in uppercase.
public
mixed
UPPERCASE
= 'uppercase'
URL
The field under this rule must be a valid url format.
public
mixed
URL
= 'url'
The default is to validate the common format: any_scheme://.... You can specify specific URL schemes if you wish.
Tags
UUID
The field under this validation must be a valid UUID and not the nil UUID string.
public
mixed
UUID
= 'uuid'