Change the file app/Exceptions/Handler.php
and add the following lines:
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Validation\ValidationException && $request->ajax())
{
return response()->json(['success' => false, 'detail' => (string) $e], 422);
}
return parent::render($request, $e);
}
Thus, when checking whether an exception is actually an instance of ValidationException
and the request corresponds to a request XhttpRequest
, you can issue a customised response as desired.
I had already explained this in that reply:
Laravel - Test script errors to return certain status to AJAX
Related: http://answall.com/a/127960/4995
– Wallace Maxters
Exemplifie what you want to do, maybe with that you’ll be clearer.
– user46523
You can create the validation manually: https://laravel.com/docs/5.2/validation#other-validation-approaches
– vmartins
It is not about creating manual validation, but about dealing with the return of the validation.
– Wallace Maxters