骨干模型验证问题

时间:2013-11-28 04:41:50

标签: javascript validation backbone.js

我计划使用http://thedersen.com/projects/backbone-validation/#examples进行模型/表单验证。但模型继续保存,而表单无效。有什么问题?

_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);

M=Backbone.Model.extend({
    validation:{
        name:{
            required:true
        }
    },
    url:'foo'
})

m=new M();
m.validate();      //return correct validation error.
m.validationError; // this is null while it should be filled by above error
m.save();          // it communicate with server while the model is not valid

1 个答案:

答案 0 :(得分:0)

尚未设置属性名称。请尝试以下代码。

M = Backbone.Model.extend({
    defaults: {
        name: null
    },
    validation:{
        name:{
            required:true
        }
    },
    url:'foo'
});