Credentials

I have a basic Ember application and am trying to handle validation errors on save (the model uses the REST Adapter). In my route, I do:

task.save().then( function() {alert("success");}, function() {alert("fail");} ).catch( function() {alert("catch error");} ); 

When the record is valid, I get a success warning, but when the record is invalid, I don't get a fail or catch error warning. In the console, I get:

 POST http://localhost:8080/api/tasks 422 (Unprocessable Entity) Error: The adapter rejected the commit because it was invalid 

The answer from api is as follows:

 {"errors":{"name":["can't be blank"],"parent_task":[]}} 

I am using Ember Data 1.13.

+1
source share
1 answer

You need to expand the adapter for error handling, the REST adapter does NOT do this for you (Active Model only)

Something like that:

 App.ApplicationAdapter = DS.RESTAdapter.extend({ ajaxError: function(jqXHR) { var error = this._super(jqXHR); if (jqXHR && jqXHR.status === 422) { var response = Ember.$.parseJSON(jqXHR.responseText), errors = {}; if (response.errors !== undefined) { var jsonErrors = response.errors; Ember.EnumerableUtils.forEach(Ember.keys(jsonErrors), function(key) { errors[Ember.String.camelize(key)] = jsonErrors[key]; }); } return new DS.InvalidError(errors); } else { return error; } } }); 
+2
source

All Articles