Reputation: 25267
I am using Backbone 0.9.10.
var Person = Backbone.Model.extend({
defaults: {
name: "John Doe",
age: 30
},
validate: function (attrs) {
if (attrs.age < 0) {
console.log("Age must be positive, idiot!");
};
}
});
If I do this on the console...
var p = new Person;
p.set("age", -20, {validate: true});
...my model gets updated anyway. How can I prevent that?
I know there's an issue open, but is there any workaround? Or do I need to wait for a bugfix?
Upvotes: 0
Views: 61
Reputation: 59763
The problem is that your call to set
uses the attribute name Age
(with a capital A
) rather than age
.
p.set("age", -20, {validate: true});
Also, when the validate
fails, you should return something other than undefined
.
validate: function (attrs) {
if (attrs.age < 0) {
return "Be more positive!";
}
}
Upvotes: 3