整个骨干模型的复杂验证

时间:2014-10-27 09:15:57

标签: validation backbone.js

我使用主干的插件backbone.validation进行模型字段验证。我喜欢插件引入的很好的声明性验证方式。我缺少的是验证不是特定领域的方法,而是整个模型。这就是我想要的:

var SomeModel = Backbone.Model.extend({
  validation: {
    field1: {
      required: true
    },
    field2: {
      required: true
    },
    field3: {
      required: true
    }
    'whole model': function () {
      // this method makes some complex validation logic and expected to be 
      // called each time the validation of model (some fields) is performed
    }
  }
});

当我不使用backbone.validation插件时,我可以很容易地做到这一点,但是当我使用它时怎么做呢?

1 个答案:

答案 0 :(得分:0)

你可以这样做,见这里:https://github.com/thedersen/backbone.validation#validate

"You can also call it manually without any parameters to validate the entire model."

插件会在幕后覆盖Backbone.Model.prototype.validate。如果您声明了所有规则并致电modelInstance.validate(),则应验证所有属性。