Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Meteor中的自定义验证简单模式中检查布尔值是否为true_Javascript_Meteor_Meteor Autoform_Meteor Collection2_Simple Schema - Fatal编程技术网

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

Javascript 如何在Meteor中的自定义验证简单模式中检查布尔值是否为true,javascript,meteor,meteor-autoform,meteor-collection2,simple-schema,Javascript,Meteor,Meteor Autoform,Meteor Collection2,Simple Schema,我有以下模式: Games.attachSchema(new SimpleSchema({ title: { type: String, label: "Title", max: 30 }, multiplayer: { type: Boolean, label: "Multiplayer", denyUpdate: true }, description: {

我有以下模式:

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
。如果未选中该复选框,则不应强制填写
说明


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

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

if (multiplayer.isSet && multiplayer.value && this.isSet && this.value == "")
return "Description is empty!";

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

if (multiplayer.isSet && multiplayer.value && this.isSet && this.value == "")
return "Description is empty!";
我找到了正确的解决方法,我这样解决它:

{
  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";
        }
      }
    }
  }
}
我找到了正确的解决方法,我这样解决它:

{
  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";
        }
      }
    }
  }
}

我不知道meteor,但是文本
“Description is empty!”
是否显示在某个地方?问题可能是返回的字符串的计算结果为
true
。我会尝试使用
返回false改为测试用例。感谢您的帮助!根据:执行任何必要的自定义验证,如果确定该值无效,则返回描述错误类型的字符串。任何非字符串返回值都表示该值有效。您的表单提交/验证逻辑是什么?我不理解您的问题。我的表单验证逻辑在我的
自定义
函数中。好的,我想知道您是否有其他用于验证/提交表单的代码。我不知道meteor对不起:我不知道meteor,但是文本
“Description is empty!”
是否显示在某个地方?问题可能是返回的字符串的计算结果为
true
。我会尝试使用
返回false改为测试用例。感谢您的帮助!根据:执行任何必要的自定义验证,如果确定该值无效,则返回描述错误类型的字符串。任何非字符串返回值都表示该值有效。您的表单提交/验证逻辑是什么?我不理解您的问题。我的表单验证逻辑在我的
自定义
函数中。好的,我想知道您是否有其他用于验证/提交表单的代码。我不知道meteor抱歉:我想大概相当于我的代码-除了我打错了:)
this.value!=“
应该是
this.value==”
。我认为大致相当于我的代码-只是我打了个错字:)
this.value!=“
应该是
this.value==”