<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class MyRequest
* @package App\Http\Requests
*/
class MyRequest extends FormRequest
{
public function rules(): array
{
return []; // your rules here
}
protected function failedValidation(Validator $validator)
{
throw new HttpResponseException(response()->json([
'status' => 'ERROR', // anything you like
'errors' => $validator->errors()
], 422));
}
}
If you are building a REST API then add the headers Accept: application/json
to your request.