accepted
Validated fields must be yes
, on
, 1
, or true
. This 服务条款
is useful for confirming consent.
active_url
Equivalent to using a PHP function dns_get_record
, the field being validated must have a valid A
or AAAA
record.
after:date
Validated fields must have values after the given date. This date will PHP
be strtotime
validated by the function.
'start_date' => 'required|date|after:tomorrow'
You can also specify other fields to compare dates:
'finish_date' => 'required|date|after:start_date'
after_or_equal:date
Validated fields must be equal to values after the given date. See after
Rules .
alpha
Validated fields must be entirely alpha characters.
alpha_dash
Validated fields may have 字母
, 数字
, dashes, -
and underscores _
.
alpha_num
Validated fields must be completely alphanumeric.
array
Validated fields must be an PHP
array .
before:date
Validated fields must have values before the given date. This date will PHP
be strtotime
validated by the function.
before_or_equal:date
Validated fields must have values on or before the given date. This date PHP
will strtotime
be validated using the function.
between:min,max
The size of the field being validated must be between the given min
and max
. String, number, array, or file size calculations are all evaluated with the size
method .
boolean
Validated fields must be convertible to boolean values. Acceptable parameters are true
, false
, 1
, 0
, "1"
and "0"
.
confirmed
The field to be validated must match foo_confirmation
the field value of . For example, if the field to be validated is password
, a matching password_confirmation
field .
date
Validated field values must PHP
be strtotime
valid dates validated by the function.
date_equals:date
Validated fields must be equal to the given date. The date will be passed to the PHP
function strtotime
.
date_format:format
Validated fields must match the given format. You should only use date
either or date_format
for validation, not both.
different:field
The validated field value must be different from the field field
's value.
digits:value
Validated fields must be numbers and must have exact values.
digits_between:min,max
The length of the field being validated must be between the given min
and max
.
dimensions
The verified file must be an image and the image ratio must comply with the rules:
'avatar' => 'dimensions:min_width=100,min_height=200'
The available rules are: min_width
, max_width
, min_height
, max_height
, width
, height
, ratio
.
Proportions should be constrained by dividing the width by the height. This can be 3/2
done with a statement like 1.5
this or a floating point constraint like this:
'avatar' => 'dimensions:ratio=3/2'
Since this rule requires multiple parameters, you can construct a more readable rule using the Rule::dimensions
method :
use Illuminate\Validation\Rule; Validator::make($data, [ 'avatar' => [ 'required', Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2), ], ]);
distinct
When validating an array, the specified field cannot have any duplicate values.
'foo.*.id' => 'distinct'
Validated fields must conform to the e-mail
address format.
exists:table,column
Validated fields must exist in the given database table.
Basic usage of the Exists rule
'state' => 'exists:states'
Specify the custom field name
'state' => 'exists:states,abbreviation'
if you need to specify the database the exists
method is used to query. 点
You can do this by prepending the name of the database to the data table using the syntax:
'email' => 'exists:connection.staff,email'
If you want to customize the query that validation rules execute, you can use the Rule
class to define the rules. In this example, we use an array to specify the validation rules, instead of separating them with the |
character :
use Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::exists('staff')->where(function ($query) { $query->where('account_id', 1); }), ], ]);
file
Validated fields must be successfully uploaded files.
filled
Validated fields cannot be empty when they exist.
image
The validated file must be an image jpeg
, png
, bmp
, gif
, or svg
.
in:foo,bar,...
Validated fields must be included in the given list of values. Since this rule usually requires you to have implode
an array, Rule::in
methods can be used to construct the rule:
use Illuminate\Validation\Rule; Validator::make($data, [ 'zones' => [ 'required', Rule::in(['first-zone', 'second-zone']), ], ]);
in_array:anotherfield
The field being validated must exist within anotherfield
the .
integer
Validated fields must be integers.
ip
The validated field must be an IP
address .
ipv4
The validated field must be an IPv4
address .
ipv6
The validated field must be an IPv6
address .
json
Validated fields must be valid JSON
strings .
max:value
Fields in validation must be less than or equal to value
. String, number, array, or file size calculations are all evaluated with the size
method .
mimetypes:text/plain,...
Validated files must match one of the given MIME
types :
'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime'
To determine the MIME
type , the content of the file is read to determine the MIME
type, which may be different from the MIME
type .
mimes:foo,bar,...
Validated files must have a MIME
type .
Basic MIME Rule Usage
'photo' => 'mimes:jpeg,bmp,png'
Even though you may only need to validate a specified extension, this rule actually validates the file's MIME
type by reading the file's content to guess its MIME
type.
This process appears to only require you to specify the extension, but in fact the rule is validated by reading the contents of the file and determining MIME
its type.
A complete list of MIME
types :
https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
min:value
Fields in validation must have a minimum value. String, number, array, or file size calculations are all evaluated with the size
method .
nullable
Validated fields can be null
. This is especially useful when validating basic data types such as strings and integers that can contain null values.
not_in:foo,bar,...
Validated fields cannot be included in the given list of values. Rule::notIn
Methods can be used to build rules:
use Illuminate\Validation\Rule; Validator::make($data, [ 'toppings' => [ 'required', Rule::notIn(['sprinkles', 'cherries']), ], ]);
numeric
Validated fields must be numbers.
present
Validated fields must exist in the input data, but can be empty.
regex:pattern
Validated fields must match the given regular expression.
Note: When using regex
rules , you must use arrays, not |
delimiters, especially if the regular expression contains |
characters .
required
Validated fields must exist in the input data and not be empty. A field is considered if one of the following conditions is true 空
:
The value is null
.
The value is an empty string.
The value is an empty array or an empty countable object.
The value is an uploaded file without a path.
required_if:anotherfield,value,...
If the other fields specified are anotherfield
equal to any value
of , the field to be validated must exist and not be empty.
required_unless:anotherfield,value,...
The field to be validated does not have to exist if the other fields specified are anotherfield
equal to any of .value
required_with:foo,bar,...
The field being validated must exist and cannot be empty as long as any of the other fields specified exist.
required_with_all:foo,bar,...
The field being validated must exist and cannot be empty only if all other specified fields are present.
required_without:foo,bar,...
The field being validated must exist and not be empty as long as any of the other specified fields are not present.
required_without_all:foo,bar,...
The field being validated must exist and not be empty only if all other specified fields are not present.
same:field
The given field must match the field being validated.
size:value
The field being validated must have a size that matches the given value. For strings, value
corresponds to the number of characters. For numbers, value
corresponds to the given integer value. For an array, it size
corresponds to the count
value . For files, it size
corresponds to the file size (unit kb
).
string
Validated fields must be strings. If you want to allow the value of this field null
, append a nullable
rule to this field.
timezone
The field to be validated must be a valid time zone identifier, which will PHP
be timezone_identifiers_list
determined according to the function.
unique:table,column,except,idColumn
Validated fields must be unique within a given database table. If not specified column
, the name of the field itself will be used.
Specify custom field names:
'email' => 'unique:users,email_address'
custom database connection
From time to time, you may need to set up custom connections for database queries created by the validator. In the above example, unique:users
setting as the validation rule is equivalent to querying the database using the default database connection. If you want to modify it, 点
specify the connection and table name using the syntax:
'email' => 'unique:connection.users,email_address'
Force the Unique
rule ignore specifying I
D:
If you want to ignore the specification when doing field uniqueness validation ID
. For example, the 更新个人资料
page will contain username, email and location. At this point you will want to verify that the updated E-mail
value is unique. If the user only changed the username field and not the E-mail
field , there is no need to throw a validation error because the user is already E-mail
the owner of this .
Use a Rule
class definition rule to instruct the validator to ignore the user's ID
. In this example, validation rules are specified as arrays, rather than separated by |
characters :
use Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::unique('users')->ignore($user->id), ], ]);
If your data table uses a different primary key name id
, specify the field name when calling the ignore
method :
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
Add an extra Where
statement :
You can also specify additional query conditions through the where
method . For example, we add account_id
as 1
the constraint of :
'email' => Rule::unique('users')->where(function ($query) { $query->where('account_id', 1); })
url
Validated fields must be valid URL
.