I have a Backbone Model with validation logic. I have bound the following method to show the field errors when the validation fails:
this.model.on('error', this.showError);
When I try to set invalid values to my model, the error event is getting fired properly.
this.model.set(invalidValues); // triggers showError method
But if I try to save the object using the same invalid values, the validation fails as expected, but the error event is not getting fired, hence the showError method is not getting called..
this.save(invalidValues, {error:this.failed,success:this.succeeded}); // does not trigger showError
The save does not trigger the error event, but the this.failed is getting called as expected.
So, what do I need to do to make sure the that the on error event is always triggered when there’s a validation error?
When you set the error callback in your save options it overrides the default of backbone, which is to trigger the error event. In your error callback, you can trigger the error event on the model or just call your showError method.