Reputation: 3317
I'm using the Backbone.Validation plugin. I have a model that does a uniqueness check that is done server-side. How would I get backbone.validation to recognize the errors and run the callbacks I have set up to display the error messages returned?
this.model.save()
.done(function(model) {
App.currentRouter.navigate('#/', {trigger: true});
})
.fail(function(response) {
alert('errors: ' + JSON.stringify(response));
// ? have backbone.validation handle errors ?
});
Thanks in advance!
Upvotes: 3
Views: 944
Reputation: 3317
So not sure if this is the best way to do this but I found a solution that works.
this.model.save()
.done(function(model, response, options) {
App.protocols.add(model);
App.currentRouter.navigate('#/', {trigger: true});
})
.fail(function(response, xhr, options) {
var errors = JSON.parse(response.responseText)['errors'];
_.each(errors, function(val, attr){
Backbone.Validation.callbacks.invalid(self, attr, val, 'name');
});
self.model.trigger('validated', false, self.model, errors);
self.model.trigger('validated:invalid', self.model, errors);
});
Upvotes: 4