Nix Nix - 6 months ago 154
Ajax Question

Form validation with ajax and laravel 5.2

I'm using ajax to submit my form. The issue I'm having is that if I have 2 or more errors that need to be displayed, I can't seem to get them to be displayed separately. The errors show up on 1 line.

This is my ajax code.

$('.ceromony_form').submit(function(event){
event.preventDefault();
$.ajax({
url: 'ceromony_post',
type: "post",
data: {'name':$('input[name=name]').val(),
'surname':$('input[name=surname]').val(),
'email':$('input[name=email]').val(),
'attendance':$('input[type=radio]:checked').val(),
'no_guest':$('input[name=no_guest]').val(),
'guests':$('textarea[name=guests]').val(),
'_token': $('input[name=_token]').val()
},
success:function(data){
$(".success_wrapper").append('<div class="success">'+data.message+'</div>');
$(".ceromony_form")[0].reset();
},
error: function(xhr) {
var errors = xhr.responseJSON;
var test = [errors.name[0], errors.surname[0]];

if(test){
$(".error_wrapper ul").html('<li class="error">'+test.join('\n')+'</li>');
}
}
});
});

Answer

Use a brake

 $(".error_wrapper ul").html('<li class="error">'+test.join('</br>')+'</li>');