首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Meteor中的自定义验证简单模式中检查布尔值是否为真

如何在Meteor中的自定义验证简单模式中检查布尔值是否为真
EN

Stack Overflow用户
提问于 2015-10-01 14:21:04
回答 2查看 919关注 0票数 1

我有以下模式:

代码语言:javascript
复制
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填写。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-01 16:49:26

我找到了合适的文档,就这样解决了它:

代码语言:javascript
复制
{
  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";
        }
      }
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2015-10-01 14:56:01

我认为问题在于你的验证逻辑。尝试将其更改为:

代码语言:javascript
复制
if (multiplayer.isSet && multiplayer.value && this.isSet && this.value == "")
return "Description is empty!";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32889971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档