0

Hello i know how to get the errors from a laravel validation but now my problem is displaying them. I want a simple way of looping through each error message and displaying on the users page using ng-repeat.

This is a sample of my code:

var req = {
    method: 'POST',
    url: '/customer',
    headers: {
        'X-XSRF-Token': $("meta[name='csrf_token']").attr("content")
    },
    data: {
        fullname: $scope.customer.input.fullname,
        address: $scope.customer.input.address,
        telephone: $scope.customer.input.telephone,
        email: $scope.customer.input.email,
        city: $scope.customer.input.city
    }
}

$http(req)
    .success(function (data, status, headers, config) {
        if (data.url !== undefined)
        {
            window.location.href = data.url;
        }
    })
    .error(function (data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        //alert(data);
    });

Example of returned object:

{"email":["The email has already been taken.","Email field is required"],"password":["The password must be at least 8 characters
."]}

How would i loop through an object like that and display each message vertically on the users page. I use bootstrap css framework.

1 Answer 1

1

Get the distinct messages into an array and display them as you want:

var obj = {"email":["The email has already been taken.","Email field is required"],"password":["The password must be at least 8 characters."]};

var distinctMsgs = [];

for(var prop in obj)
{
   if(obj.hasOwnProperty(prop))
   {
        obj[prop].forEach(function(msg){
            if(distinctMsgs.indexOf(msg) == -1)
            {
                  distinctMsgs.push(msg);
            }
        });
   }
}

console.log(distinctMsgs);

JSFIDDLE.

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

Comments

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.