laravel available validation rules


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 Aor AAAArecord.

after:date

Validated fields must have values ​​after the given date. This date will PHPbe strtotimevalidated 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 afterRules .

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 PHParray .

before:date

Validated fields must have values ​​before the given date. This date will PHPbe strtotimevalidated by the function.

before_or_equal:date

Validated fields must have values ​​on or before the given date. This date PHPwill strtotimebe validated using the function.

between:min,max

The size of the field being validated must be between the given minand max. String, number, array, or file size calculations are all evaluated with the sizemethod .

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_confirmationthe field value of . For example, if the field to be validated is password, a matching password_confirmationfield .

date

Validated field values ​​must PHPbe strtotimevalid dates validated by the function.

date_equals:date

Validated fields must be equal to the given date. The date will be passed to the PHPfunction strtotime.

date_format:format

Validated fields must match the given format. You should only use dateeither or date_formatfor 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 minand 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/2done with a statement like 1.5this 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::dimensionsmethod :
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'

email

Validated fields must conform to the e-mailaddress 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 existsmethod 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 Ruleclass 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 implodean array, Rule::inmethods 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 anotherfieldthe .

integer

Validated fields must be integers.

ip

The validated field must be an IPaddress .

ipv4

The validated field must be an IPv4address .

ipv6

The validated field must be an IPv6address .

json

Validated fields must be valid JSONstrings .

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 sizemethod .

mimetypes:text/plain,...

Validated files must match one of the given MIMEtypes :

'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime'
To determine the MIMEtype , the content of the file is read to determine the MIMEtype, which may be different from the MIMEtype .

mimes:foo,bar,...

Validated files must have a MIMEtype .

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 MIMEtype by reading the file's content to guess its MIMEtype.

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 MIMEits type.

A complete list of MIMEtypes :

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 sizemethod .

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::notInMethods 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 regexrules , 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 anotherfieldequal to any valueof , 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 anotherfieldequal 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, valuecorresponds to the number of characters. For numbers, valuecorresponds to the given integer value. For an array, it sizecorresponds to the countvalue . For files, it sizecorresponds 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 nullablerule to this field.

timezone

The field to be validated must be a valid time zone identifier, which will PHPbe timezone_identifiers_listdetermined 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:userssetting 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 Uniquerule ignore specifying ID:

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-mailvalue is unique. If the user only changed the username field and not the E-mailfield , there is no need to throw a validation error because the user is already E-mailthe owner of this .

Use a Ruleclass 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 ignoremethod :

'email' => Rule::unique('users')->ignore($user->id, 'user_id')
Add an extra Wherestatement :

You can also specify additional query conditions through the wheremethod . For example, we add account_idas 1the constraint of :

'email' => Rule::unique('users')->where(function ($query) { $query->where('account_id', 1); })

url

Validated fields must be valid URL.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768389&siteId=291194637