代码之家  ›  专栏  ›  技术社区  ›  Jems

Laravel验证总是从API返回200 OK

  •  2
  • Jems  · 技术社区  · 7 年前

    200 OK

    请求验证

    class PostRequest extends FormRequest
    {
        use Types;
    
        public function authorize()
        {
            return true;
        }
    
        public function rules()
        {
            // $auth = $this->request->user();
            return [
                'name'       => 'required|string|max:255',
                'country_id' => 'required|exists:countries,id',
                'city'       => 'required|max:255',
                'phone_no'   => 'required|regex:/[0-9]{10,13}/',
                'occupation' => 'required|max:255'
            ];
        }
    }
    

    发送器控制器

    public function store(PostRequest $request)
    {
        $auth = $request->user();
    
        $sender = Sender::create([
            'name'          => $request->name,
            'country_id'    => $request->country_id,
            'city'          => $request->city,
            'phone_no'      => $request->phone_no,
            'occupation'    => $request->occupation
        ]);
        return new Resource($sender);
    }
    

    当我发送请求时 name $validator->errors() 当我忘记输入名字的时候。我该怎么做?

    Postman

    路由和呼叫 :

    Route::post('sender', 'V1\SenderController@store');
    
    POST: localhost:8000/api/v1/sender
    
    6 回复  |  直到 7 年前
        1
  •  30
  •   Tspoon    6 年前

    你应该确保你发送的请求与 Accept: application/json 标题。

    如果没有这一点-Laravel将无法检测到它是一个API请求,也不会生成带有错误的422响应。

        2
  •  4
  •   Xartrick    6 年前

    FormRequest 要覆盖的对象 failedValidation

    如前面评论部分所述,注入 表单请求

    表单请求 和覆盖 .

    <?php
    
    namespace App\Http\Requests;
    
    use Illuminate\Contracts\Validation\Validator;
    use Illuminate\Foundation\Http\FormRequest;
    use Illuminate\Http\Exceptions\HttpResponseException;
    use Illuminate\Http\Response;
    
    abstract class BaseFormRequest extends FormRequest
    {
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        abstract public function rules(): array;
    
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        abstract public function authorize(): bool;
    
        /**
         * Handle a failed validation attempt.
         *
         * @param  Validator  $validator
         *
         * @return void
         */
        protected function failedValidation(Validator $validator): void
        {
            $errors = $validator->errors();
    
            throw new HttpResponseException(response()->json([
                'errors' => $errors
            ], Response::HTTP_UNPROCESSABLE_ENTITY));
        }
    }
    

    BaseFormRequest

    https://medium.com/@Sirolad/laravel-5-5-api-form-request-validation-errors-d49a85cd29f2

        3
  •  2
  •   Jems    7 年前

    我通过在我的系统中添加更多的代码来解决这个问题 PostRequest.php

    public $validator = null;
    protected function failedValidation($validator)
    {
        $this->validator = $validator;
    }
    

    并用控制器显示错误信息

    if (isset($request->validator) && $request->validator->fails()) {
            return response()->json([
                'error_code'=> 'VALIDATION_ERROR', 
                'message'   => 'The given data was invalid.', 
                'errors'    => $request->validator->errors()
            ]);
        }
    
        4
  •  2
  •   Md. Mohaiminul Hasan    4 年前

    如果你是从邮递员的要求,然后总是检查标题

    Accept = 'application/json'
    
        5
  •  1
  •   George Hanson    7 年前

    你的邮递员回复是寄回 welcome.blade.php Resource

    检查您提出请求的路线是否绝对正确。同时检查您是否正在进行 POST GET

        6
  •  -2
  •   Shailendra Gupta    7 年前

    保存前必须验证请求

    public function store(PostRequest $request)
    {
        $auth = $request->validate();
    
        if($auth->fails())
        {
             return response()->json($auth->errors());
        }
        $sender = Sender::create([
            'name'          => $request->name,
            'country_id'    => $request->country_id,
            'city'          => $request->city,
            'phone_no'      => $request->phone_no,
            'occupation'    => $request->occupation
        ]);
         return new Resource($sender);
    
    }
    
    推荐文章