backbone.validation
backbone.validation copied to clipboard
FAQ - What gets validated when?
How to validate just the one's that are set. Is there another method other than model.isValid(true) or model.validate() which would not validates the complete model. Here are some config that is there in my code
_.extend Backbone.Model.prototype, Backbone.Validation.mixin
Backbone.Validation.configure
forceUpdate: true
setting one attribute with {validate:true} is triggering validate on the entire model
Thanks
I'm having the same issue. I only want callbacks on the view for the attributes that are passed to validate.
For example:
model.validate({ subject: this.getValue() })
In this code, where "this" is a view widget for the subject attribute, I only expect the Backbone.Validation.callbacks to be called with valid or invalid for just the "subject" attribute. Instead I'm getting called for every attribute on the model.
This is a very bad design, assuming that the model has been changed by the view. In my case, it is important that the model is only updated when the "save" button is clicked. I validate each field on blur of the widget. So I want to show errors based on the form values, not the model attributes.
Check out prevalidate
, let's you pass a hash of values (with model properties as their keys) to a model and it only validates the given keys. It then returns whatever validate
would return, so a null/undefined on validation pass and anything else on failure.
This alternative https://github.com/fantactuka/backbone-validator can do so.