4 ms·
From the changelog: > Model validation is now only enforced by default in Model#save and no longer enforced by default upon construction or in Model#set, unles
by hueyp 14y ago
From the changelog:
> Model validation is now only enforced by default in Model#save and no longer enforced by default upon construction or in Model#set, unless the {validate:true} option is passed.
From the documentation on Model#set:
> If the model has a validate method, it will be validated before the attributes are set, no changes will occur if the validation fails, and set will return false. Otherwise, set returns a reference to the model. You may also pass an error callback in the options, which will be invoked alongside the "error" event, should validation fail.
Is the documentation out of sync, or do I not understand the changelog correctly?
- jashkenas 14y agoThanks for catching that. I've removed the outdated paragraph and republished the docs. If you notice anything else amiss, please let me know.
- hueyp 14y agoThanks, I was honestly confused, not trying to nit pick. --- Anyone know the best way to make it so set defaults to {validate:true}? Override Backbone.Model.set, extend options and call original?
- philfreo 14y agoSure (though I recommend just updating your code instead...) Only reason it's tricky is because there are two different forms of attributes that can be passed to set. Something like this should work: var oldSet = Backbone.Model.prototype.set; Backbone.Model.prototype.set = function(key, val, options) { if (key == null) return this; if (typeof key === 'object') { options = val; } options || (options = {}); options.validate = true; oldSet.call(this, key, val, options); };