jashkenas / backbone

Give your JS App some Backbone with Models, Views, Collections, and Events
http://backbonejs.org
MIT License
28.1k stars 5.39k forks source link

Validation won't stop set #2698

Closed pkhodaveissi closed 11 years ago

pkhodaveissi commented 11 years ago

backbone validate does not stop set from setting the attribute, what should I do to make it do so? isValid shows the correct result( which is false)...

var Person = Backbone.Model.extend({ defaults: { name: 'John Doe', age: 30, occupation: 'worker' },

validate: function(attrs) {
    if ( attrs.age < 0 ) {  
        return 'Age Must Be POSITIVE';
    }
},

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

});

tgriesser commented 11 years ago

As mentioned in the validate section passing {validate: true} into the options for set will do the trick - validate is only defaulted on save.

pkhodaveissi commented 11 years ago

isn't the default for the validate(attr, option) is that the option is "validate: true" ?

tgriesser commented 11 years ago

No, it's only defaulted to true on save - otherwise you need to say this.set(attrs, {validate: true})

That was changed as of 0.9.10: http://backbonejs.org/#changelog

pkhodaveissi commented 11 years ago

thanks for the fast response