骨干模型验证问题

Backbone model validation issue

本文关键字:问题 验证 模型      更新时间:2023-11-13

我计划使用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

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

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