0
驗證工作正常,但東西是不正確:骨幹驗證
var User = Backbone.Model.extend({
validate: function(attr, options){
var errors = [];
if(attr.age < 18){
errors.push('too young');
}
if(attr.firstname === ''){
errors.push('name required');
}
if(errors.length === 0){}
else{
return errors;
}
},
urlRoot: '/users'
});
如果我刪除了以下內容:「如果(errors.length === 0){}」,即使驗證是正確的(在這種情況下,無效不應該叫),「user.save不確實」似乎不工作:
saveUser: function (ev){
var userDeatils = $(ev.currentTarget).serializeObject();
var user = new User({validate:true});
user.on("invalid", function(model, error) {
if(error[0]){
errorAge(error[0]);
}
if(error[1]){
inputRequired(error[1]);
}
});
user.save(userDeatils,{
success: function(user){
router.navigate('', {trigger:true});
}
})
return false;
},
全碼here(對於只顯示代碼)