Skip to content

Instantly share code, notes, and snippets.

@sirolad
Created September 18, 2017 18:46
Show Gist options
  • Save sirolad/05ad8bfc1de6b6a86549cd03b3b8ff85 to your computer and use it in GitHub Desktop.
Save sirolad/05ad8bfc1de6b6a86549cd03b3b8ff85 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Requests;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
abstract class FormRequest extends LaravelFormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
abstract public function rules();
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
abstract public function authorize();
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function failedValidation(Validator $validator)
{
$errors = (new ValidationException($validator))->errors();
throw new HttpResponseException(response()->json(['errors' => $errors
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
}
@codeistalk
Copy link

Hi

On Line 33 you should change to HttpResponseException, because this function never throws ValidationException.

Thanks for this awesome tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment