【问题标题】:How to send validation errors as JSON to view如何将验证错误作为 JSON 发送以查看
【发布时间】:2017-05-21 00:05:54
【问题描述】:

我正在构建一个 API,我正在验证我的 StoreLesson.php 中的输入字段

    public function rules()
{
    return [
        'title' => 'required',
        'body' => 'required',
    ];
}

我正在使用postman 测试 API,一切正常,但是当我发送带有空字段的 POST 请求时,在 webview 的 postman 控制台中,我被重定向到 welcom.blade.php

//LessonController.php
public function store(StoreLessons $request) 
{
    Lesson::create($request->all());

    return response()->json($validator->errors(), 422);  //i'm not getting any json with errors

    //Lesson::create(input::all());

    return $this->respondCreated('Lesson created successfully');
}

我想将验证器错误显示(返回)为 json

谢谢你

【问题讨论】:

    标签: php laravel api validation laravel-5.3


    【解决方案1】:

    像这样使用验证器:

    $validator = Validator::make($data, $rules);
    if ($validator->fails())
        return response()->json($validator);
    

    【讨论】:

    • 我现在收到了这个错误Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0 Warning: Cannot modify header information - headers already sent in Unknown on line 0 Redirecting to http://localhost:8000.
    • 我也这么认为,但我没有得到验证器的任何错误响应尝试了很多方法,但一直重定向到主页,有什么办法可以实现这一点
    • 检查vidiation是否失败,如果失败var_dump $validator然后返回。
    • 我做了 var_dump($validator);但仍然是相同的重定向,这是我的 api frocerie.in/Laravelapi/public/api/v1/lessons
    • 我不明白,api 和 store 中的验证器响应之间的关系是什么
    【解决方案2】:
    Create StoreLessons Request inside app/Http/Requests.And call this request in LessonController.php you did.
    
    ----------
         class StoreLessons extends Request
                {
                    /**
                     * Determine if the user is authorized to make this request.
                     *
                     * @return bool
                     */
                    public function authorize()
                    {
                        return true;
                    }
    
                    /**
                     * Get the validation rules that apply to the request.
                     *
                     * @return array
                     */
                    public function rules()
                    {
                         return [
                    'title' => 'required',
                    'body' => 'required',
                ];
                    }
    
    
                    /**
                     * @param array $errors
                     *
                     * @return JsonResponse
                     */
                    public function response(array $errors)
                    {
                        return response($errors);
                    }
    
                    /**
                     * @param Validator $validator
                     *
                     * @return mixed
                     */
                    protected function formatErrors(Validator $validator)
                    {
                        return $validator->errors()->all();
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 2015-02-27
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2020-06-05
      • 2014-02-09
      相关资源
      最近更新 更多