Created
September 18, 2017 18:46
-
-
Save sirolad/05ad8bfc1de6b6a86549cd03b3b8ff85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
On Line 33 you should change to HttpResponseException, because this function never throws ValidationException.
Thanks for this awesome tip.