laravel 5.5 FormRequest class is redirecting to me i need send array errors response

913 views Asked by At

I have a problem when i validate a request with a FormRequest extended class. Because is redirecting when a bad request is recived and i need a response with the validation errors.

I'm using:

  • PHP 7.1.1 (cli) (built: Jan 18 2017 18:51:14) ( ZTS MSVC14 (Visual C++ 2015) x86 ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies.
  • Laravel v5.5.2.

My FormRequest class:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BillRequest 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 [
            'testfield' => 'required'
        ];
    }
}

My Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\BillRequest;
use App\Bill;

class BillController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(BillRequest $request)
    {
        $bills = Bill::paginate(10);
        return $bills;
    }

    /**
     * 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(BillRequest $request)
    {
        $bill = new Bill($request->all());
        $bill->save();
        return response('', 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $bill = Bill::find($id);
        $bill->customer->person;
        $bill->vehicle;
        $bill->items;
        return response($bill, 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(BillRequest $request, $id)
    {
        $bill = Bill::find($id);
        $bill->fill($request->all());
        $bill->save();
        return response('', 200);
    }

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

Route (api.php):

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::group(['prefix' => 'admin' ], function () {
    Route::resource('bills', 'BillController', [
        'only' => ['index', 'update', 'show']
    ]);
});

Finally, the response with the field 'testfield' (in the request) is the JSON with the data paginated. But when i send the request without the field then redirect to localhost:8000/

3

There are 3 answers

2
Simón Farias On BEST ANSWER

I solved the problem. It's for a missing header in the request.

Content-Type:application/json
X-Requested-With:XMLHttpRequest
0
Oyewole A. Samuel On

To validate json in Laravel, check Laravel documentation https://laravel.com/docs/5.5/validation#available-validation-rules

1
Filchos On

I experience the same problem using Laravel 5.5.3

There are many people who recommend to overwrite the response method in the custom FormRequest class, anyway this doesn't seem to work any longer since the method failedValidation is called before. This method throws an Illuminate\Validation\ValidationException.

You can catch this exception in app/Exceptions/Handler.php