backbone.validation icon indicating copy to clipboard operation
backbone.validation copied to clipboard

FAQ - What gets validated when?

Open cooolbasha opened this issue 11 years ago • 3 comments

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

cooolbasha avatar Jan 21 '14 00:01 cooolbasha

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.

arobinson avatar Jan 31 '14 17:01 arobinson

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.

hjc avatar Feb 05 '14 17:02 hjc

This alternative https://github.com/fantactuka/backbone-validator can do so.

giiska avatar Feb 20 '14 04:02 giiska