Available Validation Rules
Below is a list of all available validation rules and their function:
accepted
The field under validation must be "yes"
, "on"
, 1
, or true
. This is useful for validating "Terms of Service" acceptance or similar fields.
accepted_if:anotherfield,value,...
The field under validation must be "yes"
, "on"
, 1
, or true
if another field under validation is equal to a specified value. This is useful for validating "Terms of Service" acceptance or similar fields.
active_url
The field under validation must have a valid A or AAAA record according to the dns_get_record
PHP function. The hostname of the provided URL is extracted using the parse_url
PHP function before being passed to dns_get_record
.
after:date
The field under validation must be a value after a given date. The dates will be passed into the strtotime
PHP function in order to be converted to a valid DateTime
instance:
'start_date' => 'required|date|after:tomorrow'
Instead of passing a date string to be evaluated by strtotime
, you may specify another field to compare against the date:
'finish_date' => 'required|date|after:start_date'
after_or_equal:date
The field under validation must be a value after or equal to the given date. For more information, see the after rule.
alpha
The field under validation must be entirely alphabetic characters.
Validate::make(rules: [
'city' => 'alpha'
])->check(data: [
'city' => 'Beijing'
]);
If you want to validate all Unicode alphabetic characters, you can add the `all` option. 4.4.0
Validate::make(rules: [
'city' => 'alpha:all'
])->check(data: [
'city' => '北京'
]);
alpha_dash
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
Validate::make(rules: [
'city' => 'alpha_dash'
])->check(data: [
'city' => 'shang-hai'
]);
If you want to validate all Unicode alphabetic characters, you can add the `all` option. 4.4.0
Validate::make(rules: [
'city' => 'alpha_dash:all'
])->check(data: [
'city' => '中国-上海'
]);
alpha_num
The field under validation must be entirely alpha-numeric characters.
Validate::make(rules: [
'city' => 'alpha_num'
])->check(data: [
'city' => 'China1949'
]);
If you want to validate all Unicode alphabetic characters, you can add the `all` option. 4.4.0
Validate::make(rules: [
'city' => 'alpha_num:all'
])->check(data: [
'city' => '中国1949'
]);
array
The field under validation must be a PHP array
.
If you want to verify that the array must be a number-indexed array, you can use the @keyInt
parameter
'ids' => 'array:@keyInt'
When additional values are provided to the array
rule, each key in the input array must be present within the list of values provided to the rule. In the following example, the admin
key in the input array is invalid since it is not contained in the list of values provided to the array
rule:
use Itwmw\Validate\Validate;
$input = [
'user' => [
'name' => 'Taylor Otwell',
'username' => 'taylorotwell',
'admin' => true,
],
];
Validate::make( [
'user' => 'array:username,locale',
])->check($input);
In general, you should always specify the array keys that are allowed to be present within your array. Otherwise, the validator's check
methods will return all of the validated data, including the array and all of its keys, even if those keys were not validated by other nested array validation rules.
If some keys are optional, they can be enclosed in brackets
'user' => 'array:username,locale,[admin]'
In the above rules, username
,locale
are required and admin
is optional
Note
No other parameters can be used when @keyInt
is present
before:date
The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime
function in order to be converted into a valid DateTime
instance. In addition, like the after
rule, the name of another field under validation may be supplied as the value of date
.
before_or_equal:date
The field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP strtotime
function in order to be converted into a valid DateTime
instance. In addition, like the after
rule, the name of another field under validation may be supplied as the value of date
.
between:min,max
The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size
rule.
boolean
The field under validation must be able to be cast as a boolean. Accepted input are true
, false
, 1
, 0
, "1"
, and "0"
.
chs
The field under validation must be entirely chinese characters.
chs_alpha
The field under validation must be entirely chinese and numeric characters.
chs_dash
The field under validation may have chinese and alpha-numeric characters, as well as dashes and underscores.
chs_alpha_num
The field under validation must be entirely chinese and alpha-numeric characters.
confirmed
The field under validation must have 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.
date
The field under validation must be a valid, non-relative date according to the strtotime
PHP function.
date_equals:date
The field under validation must be equal to the given date. The dates will be passed into the PHP strtotime
function in order to be converted into a valid DateTime
instance.
date_format:format
The field under validation must match the given format. You should use either date
or date_format
when validating a field, not both. This validation rule supports all formats supported by PHP's DateTime class.
decimal:min,max 4.4.0
The field under validation must be numeric and must contain the specified number of decimal places:
// Must have exactly two decimal places (9.99)...
'price' => 'decimal:2'
// Must have between 2 and 4 decimal places...
'price' => 'decimal:2,4'
declined
The field under validation must be "no", "off", 0, or false.
declined_if:anotherfield,value,...
The field under validation must be "no", "off", 0, or false if another field under validation is equal to a specified value.
different:field
The field under validation must have a different value than field.
digits:value
The field under validation must be numeric and must have an exact length of value.
digits_between:min,max
The field under validation must be numeric and must have a length between the given min and max.
dimensions
The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
'avatar' => 'dimensions:min_width=100,min_height=200'
Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.
A ratio constraint should be represented as width divided by height. This can be specified either by a fraction like 3/2
or a float like 1.5
:
'avatar' => 'dimensions:ratio=3/2'
distinct
When validating arrays, the field under validation must not have any duplicate values:
'foo.*.id' => 'distinct'
Distinct uses loose variable comparisons by default. To use strict comparisons, you may add the strict
parameter to your validation rule definition:
'foo.*.id' => 'distinct:strict'
You may add ignore_case
to the validation rule's arguments to make the rule ignore capitalization differences:
'foo.*.id' => 'distinct:ignore_case'
doesnt_start_with:foo,bar,... 4.3.0
The field under validation must not start with one of the given values.
doesnt_end_with:foo,bar,... 4.3.0
The field under validation must not end with one of the given values.
Verify that the field is in email
format
'email' => 'email'
ends_with:foo,bar,...
The field under validation must end with one of the given values.
exclude
The field under validation will be excluded from the request data returned by the check
methods.
exclude_if:anotherfield,value
The field under validation will be excluded from the request data returned by the check
methods if the anotherfield field is equal to value.
exclude_unless:anotherfield,value
The field under validation will be excluded from the request data returned by the check
methods unless anotherfield's field is equal to value. If value is null
(exclude_unless:name,null
), the field under validation will be excluded unless the comparison field is null
or the comparison field is missing from the request data.
exclude_with:anotherfield 4.2.2
The field under validation will be excluded from the request data returned by the check
methods if the anotherfield field is present.
exclude_without:anotherfield
The field under validation will be excluded from the request data returned by the check
methods if the anotherfield field is not present.
exists:table,column
The field under validation must exist in a given database table.
Basic Usage Of Exists Rule
'state' => 'exists:states'
If the column
option is not specified, the field name will be used. So, in this case, the rule will validate that the states
database table contains a record with a state
column value matching the request's state
attribute value.
Specifying A Custom Column Name
You may explicitly specify the database column name that should be used by the validation rule by placing it after the database table name:
'state' => 'exists:states,abbreviation'
Occasionally, you may need to specify a specific database connection to be used for the exists
query. You can accomplish this by prepending the connection name to the table name:
'email' => 'exists:connection.staff,email'
If additional conditions need to be added. You can write your conditions starting with the third parameter:
'email' => 'exists:connection.staff,email,account_id,1'
Conditions may be multiple, but must occur in pairs
If you want to introduce other values from the validation data, you can use
[field]
to represent
file
The field under validation must be a successfully uploaded file.
filled
The field under validation must not be empty when it is present.
gt:field
The field under validation must be greater than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size
rule.
gte:field
The field under validation must be greater than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size
rule.
id_card 4.4.0
The field being validated must be in a legal Chinese resident ID format.
image
The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
in:foo,bar,...
The field under validation must be included in the given list of values.
Validate::make([
'type' => 'required|in:save,update,delete'
])->check($data);
in_array:anotherfield.*
The field under validation must exist in anotherfield's values.
integer
The field under validation must be an integer.
{note} This validation rule does not verify that the input is of the "integer" variable type, only that the input is of a type accepted by PHP's
FILTER_VALIDATE_INT
rule. If you need to validate the input as being a number please use this rule in combination with thenumeric
validation rule.
ip
The field under validation must be an IP address.
ipv4
The field under validation must be an IPv4 address.
ipv6
The field under validation must be an IPv6 address.
mac_address
The field under validation must be a Mac address.
mobile 4.4.0
The validated field must be a legitimate Chinese cell phone number.
json
The field under validation must be a valid JSON string.
lt:field
The field under validation must be less than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size
rule.
lte:field
The field under validation must be less than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the size
rule.
lowercase 4.4.0
The field under validation must be lowercase.
max:value
The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size
rule.
max_digits:value 4.3.0
The integer under validation must have a maximum length of value.
mimetypes:text/plain,...
The file under validation must match one of the given MIME types:
'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime'
To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client's provided MIME type.
mimes:foo,bar,...
The file under validation must have a MIME type corresponding to one of the listed extensions.
Basic Usage Of MIME Rule
'photo' => 'mimes:jpg,bmp,png'
Even though you only need to specify the extensions, this rule actually validates the MIME type of the file by reading the file's contents and guessing its MIME type.
A full listing of MIME types and their corresponding extensions may be found at the following location:
https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
min:value
The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the size
rule.
min_digits:value 4.3.0
The integer under validation must have a minimum length of value.
multiple_of:value
The field under validation must be a multiple of value.
missing 4.4.0
The field under validation must not be present in the input data.
missing_if:anotherfield,value,... 4.4.0
The field under validation must not be present if the anotherfield field is equal to any value.
missing_unless:anotherfield,value 4.4.0
The field under validation must not be present unless the anotherfield field is equal to any value.
missing_with:foo,bar,... 4.4.0
The field under validation must not be present only if any of the other specified fields are present.
missing_with_all:foo,bar,... 4.4.0
The field under validation must not be present only if all of the other specified fields are present.
not_in:foo,bar,...
The field under validation must not be included in the given list of values.
Validate::make([
'type' => 'required|notIn:save,update,delete'
])->check($data);
not_regex:pattern
The field under validation must not match the given regular expression.
Internally, this rule uses the PHP preg_match
function. The pattern specified should obey the same formatting required by preg_match
and thus also include valid delimiters. For example: 'email' => 'not_regex:/^.+$/i'
.
The regularity contains `|`
You can use arrays to specify rules, or use the regex attribute in the validator to specify regular expression rules
nullable
The field under validation may be null
.
numeric
The field under validation must be numeric.
present
The field under validation must be present in the input data but can be empty.
prohibited
The field under validation must be empty or not present.
prohibited_if:anotherfield,value,...
The field under validation must be empty or not present if the anotherfield field is equal to any value.
prohibited_unless:anotherfield,value,...
The field under validation must be empty or not present unless the anotherfield field is equal to any value.
prohibits:anotherfield,...
If the field under validation is present, no fields in anotherfield can be present, even if empty.
regex:pattern
The field under validation must match the given regular expression.
Internally, this rule uses the PHP preg_match
function. The pattern specified should obey the same formatting required by preg_match
and thus also include valid delimiters. For example: 'email' => 'regex:/^.+@.+$/i'
.
The regularity contains `|`
You can use arrays to specify rules, or use the regex attribute in the validator to specify regular expression rules
required
The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
- The value is
null
. - The value is an empty string.
- The value is an empty array or empty
Countable
object. - The value is an uploaded file with no path.
required_if:anotherfield,value,...
The field under validation must be present and not empty if the anotherfield field is equal to any value.
required_if_accepted:anotherfield 4.3.0
If the other field _anotherfield_
is accepted, then this validation field must exist and not be empty.
required_unless:anotherfield,value,...
The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is null
. If value is null
(required_unless:name,null
), the field under validation will be required unless the comparison field is null
or the comparison field is missing from the request data.
required_with:foo,bar,...
The field under validation must be present and not empty only if any of the other specified fields are present and not empty.
required_with_all:foo,bar,...
The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
required_array_keys:foo,bar,...
The field under validation must be an array and must contain at least the specified keys.
same:field
The given field must match the field under validation.
size:value
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the numeric
or integer
rule). For an array, size corresponds to the count
of the array. For files, size corresponds to the file size in kilobytes. Let's look at some examples:
// Validate that a string is exactly 12 characters long...
'title' => 'size:12';
// Validate that a provided integer equals 10...
'seats' => 'integer|size:10';
// Validate that an array has exactly 5 elements...
'tags' => 'array|size:5';
// Validate that an uploaded file is exactly 512 kilobytes...
'image' => 'file|size:512';
The size
rule also supports other fields, so when you need to compare them, you can write the names of other fields directly
Validate::make( [
'title' => 'string|size:length'
])->check([
'length' => 5,
'title' => 'title'
]);
Validate::make( [
'ids' => 'array|size:length'
])->check([
'length' => 3,
'ids' => [1,2,3]
]);
starts_with:foo,bar,...
The field under validation must start with one of the given values.
string
The field under validation must be a string. If you would like to allow the field to also be null
, you should assign the nullable
rule to the field.
timezone
The field under validation must be a valid timezone identifier according to the timezone_identifiers_list
PHP function.
unique:table,column,except,idColumn
The field under validation must not exist within the given database table.
Specifying A Custom Table / Column Name
The column
option may be used to specify the field's corresponding database column. If the column
option is not specified, the name of the field under validation will be used.
'email' => 'unique:users,email_address'
Specifying A Custom Database Connection
Occasionally, you may need to set a custom connection for database queries made by the Validator. To accomplish this, you may prepend the connection name to the table name:
'email' => 'unique:connection.users,email_address'
Add additional conditions
If additional conditions need to be added. You can write your conditions after _except_
.
'email' => 'unique:connection.users,email_address,id,[id],account_id,1'
url
The field under validation must be a valid URL.
uppercase 4.4.0
The field under validation must be uppercase.
uuid
The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).
Conditionally Adding Rules
Skipping Validation When Fields Have Certain Values
You may occasionally wish to not validate a given field if another field has a given value. You may accomplish this using the exclude_if
validation rule. In this example, the appointment_date
and doctor_name
fields will not be validated if the has_appointment
field has a value of false
:
use Itwmw\Validate\Validate;
$validator = Validate::make([
'has_appointment' => 'required|boolean',
'appointment_date' => 'exclude_if:has_appointment,false|required|date',
'doctor_name' => 'exclude_if:has_appointment,false|required|string',
])->check($data);
Alternatively, you may use the exclude_unless
rule to not validate a given field unless another field has a given value:
$validator = Validate::make([
'has_appointment' => 'required|boolean',
'appointment_date' => 'exclude_unless:has_appointment,true|required|date',
'doctor_name' => 'exclude_unless:has_appointment,true|required|string',
])->check($data);
Validating When Present
In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes
rule to your rule list:
$v = Validate::make([
'email' => 'sometimes|required|email',
])->check($data);
In the example above, the email
field will only be validated if it is present in the $data
array.