Closed Tekki closed 11 years ago
Maybe this is due to a change in the latest 0.9.10 of Backbone.js, see http://backbonejs.org/#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.
And this leads to the question: Is there a way to set "validate" to true by default, so that one does not need to pass {validate:true} to every set() call?
The Validation on blur example also does't work on Backbone 0.9.10. As a (temporary) fix you have to pass { validate: true }
on model.set()
.
My solution is to wrap the _validate
method of Backbone.Model
and add validate=true to the options object:
Backbone.Model.prototype._validate = _.wrap(Backbone.Model.prototype._validate, function(func, attrs, options) {
options.validate = true;
return func.call(this, attrs, options);
});
This should be called once after Backbone is loaded.
@svankerkfort What you propose is not a temporary solution, it is how validation is done in Backbone from 0.9.10
I'm using backbone 0.9.10 and the solution of svankerkfort function to me. When you have some time can you document this on your guide ( http://thedersen.com/projects/backbone-validation/ ), i think that it's important especially for newbie as me. Thank you for this beautiful backbone extension.
It seems that in 0.9.10 forceUpdate is always turned on (I know backbone.validation isn't tested on this version yet). I have a model that contains among other things a username and an e-mail address. If I set illegal values in this way:
the validation of the e-mail address fails in 0.9.9 and 0.9.10, but in 0.9.10 set() returns true and the model contains the new values, including the illegal mail address. With 0.9.9, the model remains as expected in its original state.