javascript - Backend Validation with RESTAdapter always "The adapter rejected the commit because it was invalid" -
configuration
- backend = laravel 5.1, dingo api
- frontend = ember 1.13.6, ember data 1.13.7
- adapter = ember data restadapter
the problem
i'm trying handle backend validation errors on first ember application. when errors returned server, error:
error: adapter rejected commit because invalid @ new error (native) @ error.embererror (http://localhost:4200/assets/vendor.js:26266:21) @ error.ember$data$lib$adapters$errors$$adaptererror (http://localhost:4200/assets/vendor.js:69564:50) @ error.ember$data$lib$adapters$errors$$invaliderror (http://localhost:4200/assets/vendor.js:69637:52) @ ajaxerror (http://localhost:4200/assets/frontend.js:16:24) @ ember$data$lib$system$adapter$$default.extend.ajax.ember.rsvp.promise.hash.error (http://localhost:4200/assets/vendor.js:71327:31) @ jquery.callbacks.fire (http://localhost:4200/assets/vendor.js:3350:30) @ object.jquery.callbacks.self.firewith [as rejectwith] (http://localhost:4200/assets/vendor.js:3462:7) @ done (http://localhost:4200/assets/vendor.js:9518:14) @ xmlhttprequest.jquery.ajaxtransport.options.send.callback (http://localhost:4200/assets/vendor.js:9920:8)
the response backend has 422 unprocessable entity header, , content is:
{ "message":"couldn't save client", "errors":{ "name":["the name field required."], "email":["the email field required."] }, "status_code":422 }
tried (and failed) solutions
ember data rest adapter error handling not working
extending restadapter override ajaxerror function, mine looks this:ajaxerror: function(jqxhr) { var error = this._super(jqxhr); if (jqxhr && jqxhr.status === 422) { var jsonerrors = ember.$.parsejson(jqxhr.responsetext).errors; return new ds.invaliderror(jsonerrors); } else { return error; } }
adding catch statement save method, save action currently:
save() { var self = this; function transitiontopost(post) { self.transitiontoroute('clients.show', post); } function failure(reason) { // handle error console.log(reason); return false; } this.get('model').save().then(transitiontopost, failure).catch(failure); }
testing out activemodeladapter - not sure expecting acheive one, got desperate; result still same.
ember docs (http://guides.emberjs.com/v1.13.0/models/creating-updating-and-deleting-records/#toc_promises)
can see in controller code above, used base
please can have , advise? in advance help!
my app/adapters/application.js
:
export default ds.restadapter.extend({ ajaxerror(jqxhr) { var error, errors, jsonerrors, response; error = this._super(jqxhr); if (jqxhr && jqxhr.status === 422) { response = ember.$.parsejson(jqxhr.responsetext); errors = {}; if (response.errors != null) { jsonerrors = response.errors; ember.keys(jsonerrors).foreach(function(key) { return errors[ember.string.camelize(key)] = jsonerrors[key]; }); } return new ds.invaliderror(errors); } else { return error; } } });
combined custom servererrorsmixin
models:
export default ember.mixin.create({ isntvalid: ember.computed.not('isvalid').readonly(), adapterdidinvalidate(errors) { let recorderrors = this.get('errors'), hasprop = {}.hasownproperty; (let key in errors) { if (!hasprop.call(errors, key)) { continue; } let propertyerrors = errors[key]; if(key === 'duplicate') { if(!ember.isarray(propertyerrors)) { recorderrors .get('messages') .pushobject(propertyerrors); } else { ember.debug('it shouldnt array.'); } } else { (let e of propertyerrors) { recorderrors .get(key) .pushobject(e); } } } this._savewasrejected(); } });
works fine in ember data 1.0.0-beta.18
. if try remember extend servererrorsmixin
in model:
import servererrorsmixin '../mixins/server-errors'; export default ds.model.extend(servererrorsmixin, { // ... })
Comments
Post a Comment