在Laravel 5.4中禁用请求验证重定向 [英] Disable request validation redirect in Laravel 5.4

查看:122
本文介绍了在Laravel 5.4中禁用请求验证重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试为内部项目开发一个rest API,但是我遇到了一个问题,即当表单请求验证失败时,它会显示@index响应.

So I'm trying to develop a rest API for an internal project, and I've got an issue where when the form request validation fails, it shows the @index response.

所以我有两条路线;

Route::get('/api/clients', 'ClientController@index');
Route::post('/api/clients', 'ClientController@store');

@index列出了所有客户端,@store创建了一个新客户端,并且我在@store方法上有一个表单请求验证器,用于检查是否为该客户端提供了名称.

@index lists all clients, @store creates a new client and I've got a Form Request Validator on the @store method which checks a name is provided for the client.

我想要的是当验证器失败时,它显示带有验证错误的JSON响应.但是我认为发生的是验证失败,因此它重定向回同一页面,但是重定向是GET而不是POST,因此它列出了所有客户端.

What I want is when the validator fails, it shows a JSON response with the validation errors. But what I think it happening, is the validation fails, so it redirects back to the same page, but the redirect is GET instead of POST, so it lists all the clients instead.

我知道您可以设置标头,使其看起来像ajax请求,在其中可以正确显示JSON响应,但是我希望它显示JSON响应,而不管它是否是ajax.

I know that you can set your headers so that it looks like an ajax request, in which it will show the JSON response properly, but I want it to show the JSON response regardless of whether it's ajax or not.

我尝试覆盖无效的验证器中的response方法,我尝试将验证器中的wantsJson方法设置为返回true,这再次无效.

I've tried overriding the response method in my validator which didn't work, I've tried setting the wantsJson method in the validator to return true which again didn't work.

非常感谢您的帮助.

代码在下面...

web.php

Route::get('/api/clients', 'ClientController@index');
Route::get('/api/clients/{client}', 'ClientController@show');
Route::post('/api/clients', 'ClientController@store');
Route::put('/api/clients/{id}', 'ClientController@update');
Route::delete('/api/clients/{id}', 'ClientController@delete');

ClientController.php

namespace App\Http\Controllers;

use App\Client;
use App\Http\Requests\ClientRequest;

class ClientController extends Controller
{

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ClientRequest $request)
    {
        return Client::create([
            'title'   => request('title'),
            'user_id' => auth()->id()
        ]);
    }

ClientRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ClientRequest 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()
    {
        return [
            'title' => 'required'
        ];
    }

    /**
     * Get the failed validation response for the request.
     *
     * @param array $errors
     * @return JsonResponse
     */
     public function response(array $errors)
     {
         dd('exit'); // Doesn't work
     }
}

推荐答案

您可以尝试这样

在表单请求中首先包含以下内容

Include use first as below in your form request

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

然后

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

现在,如果您尝试验证,那么它将返回

now if you try to validate then it will return like

{
"title": [
"The  title field is required."
]
}

这篇关于在Laravel 5.4中禁用请求验证重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆