在骨干validation不validation?

嘿所以我知道骨干改变了validation,我正在努力使其工作? 任何人都知道出了什么问题 JSfiddle链接也被validation。 不明白为什么不validation。 http://jsfiddle.net/NcHTb/

码:

(function () { window.App = { Models: {}, Collections: {}, Views: {}, Templates: {}, Router: {} }; App.Models.User = Backbone.Model.extend({ defaults: { firstName: 'first', lastName: 'last', email: 'Email', phone: '222', birthday: 'date' }, validate: function (attrs) { if (!attrs.firstName) { return 'You must enter a real first name.'; } if (!attrs.lastName) { return 'You must enter a real last name.'; } if (attrs.email.length < 5) { return 'You must enter a real email.'; } if (attrs.phone.length < 10 && attrs.phone === int) { return 'You must enter a real phone number, if you did please remove the dash and spaces.'; } if (attrs.city.length < 2) { return 'You must enter a real city.'; } } }); var user = new App.Models.User(); //user.on('change', function () { // console.log('something changed'); - used for change anything //}); user.on('invalid', function (model, invalid) { console.log(invalid); }); user.on('change:firstName', function () { console.log('something changed'); }); user.set('firstName', ''); console.log(JSON.stringify(user)); console.log(user.get('birthday')); })(); 

正如@nikoshr回答的那样,这个问题已经解决了。

你基本上必须把{ validate: true }作为调用set()的选项。 save()方法总是触发validation。
最后,你的代码将是这样的:

 user.set('firstName', '', { validate: true }); // Run validations user.set('firstName', ''); // Doesn't run validations user.save(); // Run validations