thedersen / backbone.validation

A validation plugin for Backbone.js that validates both your model as well as form input
thedersen.com/projects/backbone-validation
MIT License
1.32k stars 301 forks source link

forceUpdate and Backbone 0.9.10 #105

Closed Tekki closed 11 years ago

Tekki commented 11 years ago

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:

var change = { username: 'new username', email: 'new@email' };
if (model.set(change)) {
// success
} else {
// error
}

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.

jenskl commented 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?

ba55ie commented 11 years ago

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().

berzniz commented 11 years ago

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.

thedersen commented 11 years ago

@svankerkfort What you propose is not a temporary solution, it is how validation is done in Backbone from 0.9.10

NickNaso commented 11 years ago

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.