Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone validate not working [duplicate]

Im just starting out backboneJs.

I did a simple example using VisualStudio2012. I added backbone.

in my main.js

var Person = Backbone.Model.extend({
defaults: {
    name: 'Mr Steel',
    age: 26,
    occupation: 'FrontEnd Web Developer'
},

validate: function(attrs) {
    if (attrs.age < 0) {
        return 'Age must be a positive number.';
    }
},

work: function() {
    return this.get('name') + ' is working.';
}
});

But when I go to browser > console. And type the following in .. validate doesnt even get triggered.

var person = new Person();
person.get('age');//26
person.set('age', -25);//sets -25

And it sets the age to -25. thats weird!! please help.

like image 668
STEEL Avatar asked Mar 31 '13 16:03

STEEL


1 Answers

You have to pass {validate:true} param to set method e.g.

person.set('age', -25, {validate:true});

like image 164
Vitalii Petrychuk Avatar answered Nov 15 '22 20:11

Vitalii Petrychuk