In the App\Http\Controllers\Controller file (the base controller class), there is a function that builds the failed validation response.
/**
* Create the response for when a request fails validation.
*
* @param \Illuminate\Http\Request $request
* @param array $errors
* @return \Illuminate\Http\Response
*/
protected function buildFailedValidationResponse(Request $request, array $errors)
{
if ($request->ajax() || $request->wantsJson()) {
return new JsonResponse($errors, 422);
}
return redirect()->to($this->getRedirectUrl())
->withInput($request->input())
->withErrors($errors, $this->errorBag());
}
You can extend Controller with your own controller class and override this method so it always builds the validation errors a certain way. For example this is how mine looks (I built an APIService which I use to return responses).
/**
* @inherited
*
* I'm overwriting this function because I just want to return a json response no matter what (this is an API)
* @param RequestData|Request $request
* @param array $errors
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
*/
protected function buildFailedValidationResponse(RequestData $request, array $errors)
{
return $this->api->setStatusCode(422)->respondWithError($errors);
// return new JsonResponse($errors, 422); // something like this? if you haven't built your own way to return json responses.
}
So essentially you just want to override that function and tie it into how your application sends it's json responses.