laravel data validation

The data validation provided by laravel has multiple methods, and each validation rule is separated by "|".

Validation rules

 

Rule name

explain

required

Cannot be empty

max:value

The field value must be less than or equal to value. For a string, value is the number of characters

min:value

The field value must be greater than or equal to value. For a string, value is the number of characters

email

Verify whether the mailbox is legal

url

The validation field must be a valid URL format

confirmed

Verify whether the two fields are the same. If the verified field is password, you must enter a matching password_confirmation field

integer

Validation field must be an integer

ip

The validation field must be an IP address

numeric

Validation field must be numeric

size:value

The value validation field must have a size that matches the given value. For a string, value is the corresponding number of characters, and for a value, value is the given integer value; For a file, value is the corresponding number of File Bytes

string

Validation field must be a string

unique

Table name, field, ID to be excluded

between:min,max

Verify that the size of the field value is between the specified min and max. String, numeric value or file size are calculated in the same way as the size rule

 

 

csrf processing of forms and ajax

Form processing: {{csrf_field()}}

ajax processing: {{csrf_token}}

Note: form processing and ajx submission processing are different. The difference is that the form processing will generate an additional hidden field for storing token s.

Let's create the controller and page first

 

 

 

 

 

 

 

1. Use the controller $this - > validate to validate the form

2. Validate forms independently

3. Verifier

 

1. Use $this - > validate to validate the form

What if the incoming request parameter does not pass the given validation rule? As mentioned earlier, Laravel will automatically redirect the user to the previous location. In addition, all validation error messages will be automatically stored in the session.

Again, we don't have to explicitly bind the error message to the view in the GET route. Because Lavarel will check the error information in the Session data and automatically bind it to the view (if the view file exists). The variable $errors is an instance of Illuminate\Support\MessageBag. The errors variable is bound to the view by the Illuminate\View\Middleware\ShareErrorsFromSession middleware provided by the Web middleware group. When this middleware is applied, you can GET the $error variable in your view, which can make you assume that the $errors variable exists and can be used safely.

As shown below:

 

 

 

$input = $this->validate(
            $request,
            [
                'username' => 'required|between:2,6',
                'password' => 'required|confirmed',
                'password_confirmation' => 'required',
                'email' => 'email',
            ]
        );

 

 

 

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

If you want to customize the prompt, define the third parameter

 

$input = $this->validate(
            $request,
            [
                'username' => 'required|between 2,6',
                'password' => 'required|confirmed',
                'password_confirmation' => 'required',
                'email' => 'required|email',
            ],
            [
                'username.required' => 'Account number cannot be empty',
                'username.between' => 'Account number must be 2-6 Between characters',
                'password.required' => 'Password cannot be empty',
                'password.confirm' => 'The two passwords are inconsistent',
                'password_confirmation.required' => 'Confirm password cannot be empty',
                'email.required' => 'email Cannot be empty',
                'email.email' => 'email Incorrect format',
            ]
        );

Only when the data meets the conditions, it will continue to be executed later. Assign it to input, as shown in the figure

Later, we can carry out subsequent operations. We can add the old function to leave the qualified options

 

 

2. Independent verification form

Usage scenario: if you don't want to use the {validate} method on the request and want to jump, you can use} Validator facade Manually create a validator example.

Use Validator facade Create a verifier example using the make method on:

   

 

 

 

$validator = Validator::make(
         $request->all(),
           [
               'username' => 'required|between:2,6',
               'password' => 'required|confirmed',
               'password_confirmation' => 'required',
               'email' => 'required|email',
           ],
           [
               'username.required' => 'Account number cannot be empty',
               'username.between' => 'Account number must be 2-6 Between characters',
               'password.required' => 'Password cannot be empty',
               'password.confirm' => 'The two passwords are inconsistent',
               'password_confirmation.required' => 'Confirm password cannot be empty',
               'email.required' => 'email Cannot be empty',
               'email.email' => 'email Incorrect format',
           ]
       );
        if ($validator->fails()) {
            return redirect(route('user.add'))->withErrors($validator);
        }

 

Posted by erick_w on Wed, 18 May 2022 12:45:24 +0300