我有以下模式:
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填写。
我尝试了上面的代码,但它没有验证。即使我没有描述,我选中了复选框,我也可以提交表单。
发布于 2015-10-01 16:49:26
我找到了合适的文档,就这样解决了它:
{
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-01 14:56:01
我认为问题在于你的验证逻辑。尝试将其更改为:
if (multiplayer.isSet && multiplayer.value && this.isSet && this.value == "")
return "Description is empty!";https://stackoverflow.com/questions/32889971
复制相似问题