Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

json - Disable request validation redirect in Laravel 5.4

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.

So I have two routes;

Route::get('/api/clients', 'ClientController@index');
Route::post('/api/clients', 'ClientController@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.

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.

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.

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.

Help would be very much appreciated.

Code is below...

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 AppHttpControllers;

use AppClient;
use AppHttpRequestsClientRequest;

class ClientController extends Controller
{

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

ClientRequest.php

<?php

namespace AppHttpRequests;

use IlluminateFoundationHttpFormRequest;

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
     }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try like this

Include use first as below in your form request

use IlluminateContractsValidationValidator;
use IlluminateHttpExceptionsHttpResponseException;

and then

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."
]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...