Validator’s form validation in laravel

==========================

  1. $validator = Validator::make($request->all(), [

  2. 'nickname' => 'required|max:8|string',

  3. 'signature' => 'string|max:30',

  4. 'gender' => ' integer',

  5. 'height' => ' digits_between:50,300',

  6. 'weight' => ' integer|min:2|max:3',

  7. 'birth_date' => 'date',

  8. 'city' => 'string',

  9. 'order_number' => 'exists:orders,order_number',

  10. 'openid' => 'exists:orders,openid',

  11. 'activity_id' => 'required'

  12. ]);

  13. if ($validator->fails()) {

  14. return $this->responseForJson(ERR_ACCESS_DEN, $validator->errors());

  15. }


accepted

Validation will only pass if the field value is yes, on, or 1. This is useful when confirming that the “Terms of Service” are agreed.

active_url

The field value is verified as a valid URL by the PHP function checkdnsrr.

after:date

Validates that the field is after the specified date. This date will be validated using the PHP strtotime function.

alpha

Fields only pass validation if they are all alphanumeric strings.

alpha_dash

Field values ​​only allow letters, numbers, dashes (-), and underscores (_)

alpha_num

Field values ​​only allow letters and numbers

array

Field values ​​are only allowed to be [arrays]

before:date

Verify that the field is before the specified date. This date will be validated using the PHP strtotime function.

between:min,max

The field value needs to be between the specified min and max values. Strings, numbers, or files are all validated in the same way.

confirmed

The field value needs to be the same as the corresponding field value foo_confirmation. For example, if the field to be verified is password, the corresponding field password_confirmation must exist and match the password field.

date

The field value is verified as a valid date by the PHP strtotime function.

date_format:format

The field value uses the PHP date_parse_from_format function to verify whether the date in the format specified by format is a legal date.

different:field

The field value needs to be different from the specified field field value.

digits:value

The field value needs to be a number and the length needs to be value.

digits_between:min,max

The field value needs to be a number and the length needs to be between min and max.

boolean

Field must be convertible to boolean, acceptable values ​​are true, false, 1, 0, “1”, “0”.

email

Field value must conform to email format.

exists:table,column

The field value needs to be the same as one of the column field values ​​that exist in the database table.
Basic usage of the Exists rule

'state' => 'exists:states'

Specify a custom field name

'state' => 'exists:states,abbreviation'

You can specify more conditions and those conditions will be added to the “where” query:

  1. 'email' => 'exists:staff,email,account_id,1'

Use NULL with the abbreviation “where” to check whether the database is NULL

'email' => 'exists:staff,email,deleted_at,NULL'

image

The file must be an image (jpeg, png, bmp, gif or svg)

in:foo,bar,…

The field value must match one of the values ​​in the pre-given list

integer

Field value needs to be an integer value

ip

The field value must conform to the IP address format.

max:value

The field value must be less than or equal to value. Strings, numbers and files are judged by size.

mimes:foo,bar,…

A file’s MIME class needs to be in the list in the given manifest to pass validation.
Basic usage of MIME rules

'photo' => 'mimes:jpeg,bmp,png'

min:value

The field value must be greater than or equal to value. Strings, numbers and files are judged by size.

not_in:foo,bar,…

Field value must not be one of the given list.

numeric

Field value needs to be a number.

regex:pattern

Field values ​​must conform to the given regular expression.

Note: When using regex patterns, you must use an array instead of “|” as a separator, especially when the regular expression contains “|” strings.

required

Field value is required.

required_if:field,value

Field value is required when the field field value is value.

required_with:foo,bar,…

Field values ​​are only required if any of the specified fields have a value.

required_with_all:foo,bar,…

Field values ​​are only required if all specified fields have values.

required_without:foo,bar,…

Field values ​​are only required if any of the specified fields have no value.

required_without_all:foo,bar,…

Field values ​​are only required if none of the specified fields have a value.

same:field

The field value must be equal to the specified field field.

size:value

The size of the field value must conform to the given value. For strings, value is the length of the string to match. For numbers, value is the integer value to match. For files, the value is the size of the file (unit: kb) to conform to.

timezone

Field values ​​are verified as valid timezones by the PHP timezone_identifiers_list function.

unique:table,column,except,idColumn

Field values ​​need to be unique within a given database. If the column option is not specified, the field name will be used.

Occasionally, you may need to set a custom connection for database queries made by the Validator. As seen above, setting unique:users as a validation rule will use the default database connection to query the database. To override this, do the following:

  1. $verifier = App::make('validation.presence');

  2. $verifier->setConnection('connectionName');

  3. $validator = Validator::make($input, [

  4. 'name' => 'required',

  5. 'password' => 'required|min:8',

  6. 'email' => 'required|email|unique:users',

  7. ]);

  8. $validator->setPresenceVerifier($verifier);

Basic usage of the Unique rule

'email' => 'unique:users'

Specify a custom field name

'email' => 'unique:users,email_address'

Force unique rule to ignore specified ID

'email' => 'unique:users,email_address,10'

Add additional Where conditions

You can also specify more conditions in the “where” query:

'email' => 'unique:users,email_address,NULL,id,account_id,1'

The above rule is that only the data column whose account_id is 1 will be verified by the unique rule.

url

The field value must conform to the format of the URL.

Note: This function validates using the PHP filter_var method

Authenticating requests is very simple using [Laravel] ‘s  ValidatesRequests trait, which has  BaseController been automatically included by passing. It is very powerful and provides several useful rules for common situations. The  exists() sum  unique() is used to verify whether the data exists in the database, and is usually used as follows:

  1. // exists example
  2. ’email’ => ‘exists:staff,account_id,1’
  3. // unique example
  4. ’email’ => ‘unique:users,email_address,$user->id,id,account_id,1’

As you can see, this method is not easy to remember, and sometimes you may often need to go to the document.

As of Laravel v5.3.18, both of these rules have  Rule been simplified with newly introduced classes.

Using the same example above, the same effect can be achieved with this more fluid example below:

  1. ’email’ => [
  2. ‘required’,
  3. Rule::exists(‘staff’)->where(function ($query) {
  4. $query->where(‘account_id’, 1);
  5. }),
  6. ],

  7. ’email’ => [

  8. ‘required’,
  9. Rule::unique(‘users’)->ignore($user->id)->where(function ($query) {
  10. $query->where(‘account_id’, 1);
  11. })
  12. ],

Both methods support the following methods:

  • where
  • whereNot
  • whereNull
  • whereNotNull

unique Methods contain additional  ignore methods so you can validate additional data.

Another benefit of this new feature is that the old way is still fully supported, implemented by converting to the old string way through the  formatWheres  method:

  1. protected function formatWheres()
  2. {
  3. return collect($this->wheres)->map(function ($where) {
  4. return $where[‘column’].’,’.$where[‘value’];
  5. })->implode(‘,’);
  6. }

To get the latest version you need to be running  composer update, once you get v5.3.18 you can use this new smooth way.

Update On 2016-10-16:

The demisions rule will also be added in a new way in Laravel v5.3.19:

via: laravel-news

Leave a Comment

Your email address will not be published. Required fields are marked *