Currently I use this to display validation errors via ajax:
if (data.validation_failed == 1)
{
var arr = data.errors;
Laravel 5 returns validation error automatically
for that you just need to do following thing,
public function methodName(Request $request)
{
$this->validate($request,[
'field-to-validate' => 'required'
]);
// if it's correctly validated then do the stuff here
return new JsonResponse(['data'=>$youCanPassAnything],200);
}
$.ajax({
type: 'POST',
url: 'url-to-call',
data: {
"_token": "{{ csrf_token() }}",
"field": $('#field').cal()
},
success: function (data) {
console.log(data);
},
error: function (reject) {
if( reject.status === 422 ) {
var errors = $.parseJSON(reject.responseText);
$.each(errors, function (key, val) {
$("#" + key + "_error").text(val[0]);
});
}
}
});
you can build for each validation
field one tag with id as field name and suffix
_error
so it will show validation error with above logic like as follow,
Hope it helps :)