Created
June 5, 2019 08:05
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\Api; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Contracts\Validation\Validator; | |
use Illuminate\Validation\ValidationException; | |
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