当前位置:网站首页>Laravel form data validation

Laravel form data validation

2022-07-28 12:15:00 History teacher-

1. laravel There are several methods to verify the data provided , Each validation rule uses “|” separate .

Forms and ajax Of csrf Handle

Form processing :{ {csrf_field()}}

ajax Handle :{ {csrf_token}}

  Be careful : Form processing and ajx Submission processing is different . The difference is that the form processing will generate an additional storage token The hidden domain of .

Let's create the controller and page first

 

1、 Using the controller $this->validate Perform form validation

2、 Validate the form independently

3、 Validator

1、 Use $this->validate Perform form validation

What if the incoming request parameters fail to pass the given validation rules ? As mentioned earlier ,Laravel Will automatically redirect the user to the previous location . in addition , All validation error messages will be automatically Store in session in .

Once again , We don't have to be GET Explicitly bind the error message to the view in the route . because Lavarel Will check in Session Error messages in data , And automatically bind it to the view ( If this view file exists ). And the variables $errors yes Illuminate\Support\MessageBag An example of .errors Variable is Web Provided by the middleware Group Illuminate\View\Middleware\ShareErrorsFromSession Middleware is bound to the view . When this middleware is applied , In your view, you can get $error Variable , You can always assume $errors Variables exist and can be used safely .

Here's the picture :

$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 , Just 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' => ' The password cannot be empty ',
                'password.confirm' => ' The two passwords don't match ',
                'password_confirmation.required' => ' The confirmation password cannot be empty ',
                'email.required' => 'email Can't be empty ',
                'email.email' => 'email Incorrect format ',
            ]
        );

2、 Validate the form in an independent way

  Use scenarios : If you don't want to use on requests  validate  Method , And want to jump , You can go through  Validator facade  Manually create a validator example .

      use  Validator facade  Upper  make  Method to create an example validator :

$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' => ' The password cannot be empty ',
               'password.confirm' => ' The two passwords don't match ',
               'password_confirmation.required' => ' The confirmation password cannot be empty ',
               'email.required' => 'email Can't be empty ',
               'email.email' => 'email Incorrect format ',
           ]
       );
        if ($validator->fails()) {
            return redirect(route('user.add'))->withErrors($validator);
        }

 

 

原网站

版权声明
本文为[History teacher-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281123500731.html