Laravel FormRequest used for verification and validation abnormal form custom processing

This paper shows that the individual flight and validation layer (i.e., parameter validation)

To simplify the controller parameter validation, we set up a separate folder, as we layer parameter validation; specific exception is thrown, and Form Request execution laravel official documentation is very detailed, show the following cases only their own Baidu, easy to quickly get started with:

1. Create a new folder Requests

2. Then New NavRequest, parameter validation classes, inheritance FormRequest

<?php

namespace App\Http\Requests\Nav;

use Illuminate\Foundation\Http\FormRequest;

class NavRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        $rules = [
            'h5_id' => 'required |exists:h5_template,h5_id',
            'font_color'=>'string|nullable',
            'select_color'=>'string|nullable',
            'background_color'=>'string|nullable',
            'position'=>'int|required| between:0,1'
        ];
        return $rules;
    }

    public function messages()
    {
        $message = [
            'h5_id.required'      =>'h5_id必须填写',
            'h5_id.exists'      =>'h5_id不存在!',
            'position.required'      =>'请选择位置!'
        ];
        return $message;
    }

}
  • The first method: authorize (), controlling user access
  • The second method: $ rules (), returns a valid array
  • The third method: $ message (), return a custom error messages

3. The controller inside \

Traditionally we use this, but each controller needs to determine the cause of these individual parameters. Code reuse rate is not high.

 public function navSet(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'h5_id' => 'required |int',
            'font_color'=>'string|nullable',
            'select_color'=>'string|nullable',
            'background_color'=>'string|nullable',
            'position'=>'int|required| between:0,1'
        ]);
        if ($validator->fails()) {
            return $this->output(null,'参数有误,请重试!',500);
        }
        $request_params=$request->all();

     //接下来实现你的业务逻辑....
}

When we pulled out when the parameter validation layer,

 public function navSet(NavRequest $request)//将你抽离的参数层绑定到这里
    {
          $request_params=$request->all();
       
         //接下来处理你的业务逻辑...
    }

So whatever you bind Navrequest this parameter validation class where, when, the parameters will be verified

4. Verify returns an error message

In this place I was writing interface, in any case not receive an error message and then view FoemRequest error message is returned:

See here read: the following figure.

  protected function failedValidation(Validator $validator)
    {

        throw (new ValidationException($validator))
                    ->errorBag($this->errorBag)
                    ->redirectTo($this->getRedirectUrl());
    }

Meaning redirection error message, but as the interface developers, many times we need to return to the front end of the specified json data, so we can override this method are as follows;

(Special attention, did not start rewriting planted a pit)

//1.在这里可以重新定义一个requestController类,,
//2.然后让他继承 FormRequest,
//3.最后重写FormRequest这个基类的failedvalidation方法

class RequestController extends FormRequest
{
    protected function failedValidation(Validator $validator) {
        $error= $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg'=>'error','code'=>'500','data'=>$error[0]], 500));

    }
}

So that when you do not meet the parameters of the custom validation rules, it will return an error message you customize your route in the inside.

5.postman error message display

 

Guess you like

Origin blog.csdn.net/cfun_goodmorning/article/details/83539026