Skip to content

Instantly share code, notes, and snippets.

@jamesmills
Created June 5, 2019 08:05
Show Gist options
  • Save jamesmills/3c2fe1fc0d26eefb88c00b3b729f3005 to your computer and use it in GitHub Desktop.
Save jamesmills/3c2fe1fc0d26eefb88c00b3b729f3005 to your computer and use it in GitHub Desktop.
<?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