0

With this code i get each validation error as a single alert:

$.ajax({
    type: myMethod,
    url: myRoute,
    headers: { 'X-CSRF-TOKEN': "{{csrf_token()}}" },
    data: form.serializeArray(),
    dataType: 'json',
    success: function(data){
        console.log('validated!');
    },
    error: function(data) {
        var errors = data.responseJSON;
        for (error in errors) {
            alert(errors[error][0]);
        }
        console.log(errors);
    }
});

So if there are 3 errors user would have to click to close all 3 of them and i would like to alert the error messages in one single alert.

How can i group them?

1 Answer 1

1

A very simple way would be to store them in an array and alert it.

e.g.

error: function(data) {
        var errors = data.responseJSON;
        var errorsArr = [];
        for (error in errors) {
            errorsArr.push(errors[error][0]);
        }
        alert(errorsArr);
        console.log(errors);
    }

By default alert of array would show comma separated string. Incase you want each error on new line, try alert(errorsArr.join("\n"))

Sign up to request clarification or add additional context in comments.

5 Comments

alert(errorsArr.join("\n")); this prints it out all comma separated. I would really like to have each error on a single line....how to do that?
Could you copy the result in the alert?
Oh now i know what is the problem. I am using bootbox.js plugin and it looks like this: s30.postimg.org/pi66w8lpt/Capture.png but if i use just alert(); it prints it each on a new line
Try to change .join("\n") to .join("<br/>")
Yes i found it here stackoverflow.com/questions/30322614/… Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.