How do I get http response in Restangular .then() function from Laravel Request validation

Issue

Okay, I’m using Restangular and Laravel 5.1, and I need to be able to handle failed form validation. One way would be to do it on the front end with Angular, but I’d also like to be able to handle it server side.

Currently, the request reaches my form request, and then the form request returns

{"users":["The users field is required."]}

In my Angular controller, I have the following:

Factory.update(var1, var2).then(function(results) {
    console.log("results");
});

This doesn’t return anything because it’s never reaching that point. Laravel kicks it out before then.

Thoughts?

Solution

I don’t know Laravel, but I know how HTTP should work.

Seems like Laravel is sending back an HTTP status code for error (anything above 400 is an error).

Restangular detects this status code, and then it’ll reject that promise, instead of resolving it — that’s why your console.log call is never reached.
You should do the following instead:

Factory.update(var1, var2).then(function(results) {
    console.log("results");
}, function(error) {
    console.log("oh you got errorz!");
});

Answered By – gustavohenke

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.