I am making an API and i want return the array of errors with a format as the one that $validator->errors(); generates when i validate the request by the manual way. But i cant manipulate the response. I wanna find thhe correct way to make it.

This could be done in Laravel 5.4 with the formatErrors method and including the Illuminate\Contracts\Validation\Validator class in the FormRequest class but for version 5.5 it does not work. I do not know how to do it.

This is my Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ProductRequest;
use Illuminate\Validation\Rule;
use App\Product;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(ProductRequest $request)
    {
        $products = Product::where('code', 'LIKE', '%'.$request->input('search').'%')
        ->where('name', 'LIKE', '%'.$request->input('search').'%')
        ->paginate(10);
        $products->withPath($request->fullUrl());
        return $products;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProductRequest $request)
    {
        $product = new Product($request->validated());
        $product->save();
        return response('', 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $product = Product::find($id);
        return response($product, 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ProductRequest $request, $id)
    {
        $product = Product::find($id);
        $product->fill($request->validated());
        $product->save();
        return response('', 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $product = Product::find($id);
        $product->delete();
        return response('', 204);
    }
}

This is mi FormRequest class

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProductRequest extends FormRequest
{
    /**
     * 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()
    {
        switch($this->method())
        {
            case 'GET':
            {
                return [
                    'code' => 'string',
                    'name' => 'string',
                ];
            } break;
            case 'POST':
            {
                return [
                    'code' => 'required|unique:Products,code',
                    'name' => 'required',
                    'description' => 'required',
                    'quantity' => 'required|min:0',
                    'price' => 'required|numeric',
                    'extemp' => [
                        Rule::in(['tax', 'free']),
                    ]
                ];
            } break;
            case 'PUT':
            {
                return [
                    'code' => 'unique:products,code,'.$this->route('product'),
                    'name' => 'string:min:1',
                    'description' => 'string|min:1',
                    'quantity' => 'integer|min:0',
                    'price' => 'numeric',
                    'extemp' => [
                        Rule::in(['tax', 'free']),
                    ],
                ];
            } break;
            case 'PATCH': break;
            case 'DELETE': break;
            default:
            {
                return [];
            } break;
        }
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            //Product
            'code.required' => 'El :attribute es obligatorio.',
            'code.unique' => 'El :attribute ya se encuentra registrado.',
            'name.required' => 'El :attribute es obligatorio.',
            'name.min' => 'El :attribute es obligatorio.',
            'description.required' => 'El :attribute es obligatorio.',
            'description.min' => 'El :attribute es obligatorio.',
            'quantity.required' => 'La :attribute es obligatoria.',
            'quantity.integer' => 'La :attribute debe ser un número entero.',
            'quantity.min' => 'La :attribute debe ser al menos :min.',
            'price.required' => 'El :attribute es obligatorio.',
            'price.numeric' => 'El :attribute debe ser un valor numérico.',
            'extemp.in' => 'El :attribute seleccionado es inválido.',
        ];
    }
    public function attributes(){
        return [
            'code' => 'código',
            'name' => 'nombre',
            'description' => 'descripción',
            'quantity' => 'cantidad',
            'price' => 'precio',
            'extemp' => 'exento',
        ];
    }
}

The Response what i have:

{
    "message": "The given data was invalid.",
    "errors": {
        "code": [
            "El código es obligatorio."
        ],
        "name": [
            "El nombre es obligatorio."
        ],
        "description": [
            "El descripción es obligatorio."
        ],
        "quantity": [
            "La cantidad es obligatoria."
        ],
        "price": [
            "El precio es obligatorio."
        ]
    }
}

The Response what i want (with $validator->errors();)

[
    "El código es obligatorio.",
    "El nombre es obligatorio.",
    "El descripción es obligatorio.",
    "La cantidad es obligatoria.",
    "El precio es obligatorio."
]

推荐答案

You have to override the failedValidation() method and issue the exception with the response what you want. So, you need to use Illuminate\Contracts\Validation\Validator and Illuminate\Http\Exceptions\HttpResponseException in the RequestForm class, and override the failedValidation() method:

protected function failedValidation(Validator $validator) { 
        throw new HttpResponseException(response()->json($validator->errors()->all(), 422)); 
}

Laravel相关问答推荐

查询Laravel中的图形

使用 Laravel 10 在 Blade 中添加成功消息

我在 laravel 中插入多个复选框值.我怎样才能做到这一点?

函数接受 Laravel 集合对象,尽管只允许使用字符串

如何在 Laravel 中填充以后执行的条件

Laravel 5 如何配置 Queue 数据库驱动程序以连接到非默认数据库?

Laravel 有Many Many to Many To One Eloquent

在 apache docker 容器中运行虚拟主机

Lumen和 MongoDB?

如何在 Laravel 5.5 中扩展 vendor 包服务提供者

如何更改 Laravel5 中的视图文件夹?

Laravel Eloquent Pluck 不丢失密钥

Laravel excel maatwebsite 3.1 导入,excel 单元格中的日期列返回为未知格式数字.如何解决这个问题?

Laravel 从现有模型生成迁移

laravel Blade中多选选项中的旧值

Laravel Mix 生成字体到另一个目录

在 Laravel 5 控制器中找不到类App\Http\Controllers\DB

laravel 如何读取 app/config/app.php 调试变量

如何在laravel中获取列值的平均值

Laravel 不活动时间设置