1

我有以下架构:

Games.attachSchema(new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 30
    },
    multiplayer: {
        type: Boolean,
        label: "Multiplayer",
        denyUpdate: true
    },
    description: {
        type: String,
        label: "Description",
        custom: function() {
            var multiplayer = this.field("multiplayer");
            if (multiplayer.isSet && multiplayer.value && !this.isSet) return "Description is empty!";
            return true;
        }
    }
}));

我的目标是检查是否description为空,但前提是复选框multiplayer已被选中。如果未选中复选框,则description不应强制填写。

我尝试了上面的代码,但它没有验证。即使我没有描述并且我选中了复选框,我也可以提交表单。

4

2 回答 2

0

我认为问题出在您的验证逻辑上。尝试将其更改为:

if (multiplayer.isSet && multiplayer.value && this.isSet && this.value == "")
return "Description is empty!";
于 2015-10-01T14:56:01.483 回答
0

我找到了正确的文档并像这样解决了它:

{
  description: {
    type: String,
    optional: true,
    custom: function () {
      var shouldBeRequired = this.field('multiplayer').value;

      if (shouldBeRequired) {
        // inserts
        if (!this.operator) {
          if (!this.isSet || this.value === null || this.value === "") return "required";
        }

        // updates
        else if (this.isSet) {
          if (this.operator === "$set" && this.value === null || this.value === "") return "required";
          if (this.operator === "$unset") return "required";
          if (this.operator === "$rename") return "required";
        }
      }
    }
  }
}
于 2015-10-01T16:49:26.820 回答