【发布时间】:2019-01-03 06:30:03
【问题描述】:
我有一个带有两个字段的 SimpleSchema 实例:
isApproved: {
type: Boolean,
defaultValue: false
},
impactedSystems: {
type: String,
optional: true
}
如果 isApproved 设置为 true,我想将 impactedSystems 的 optional 属性更改为 false。
我已尝试按照docs 中的说明进行操作,但无法正常工作。它建议我像这样向 impactedSystems 添加一个自定义函数(用我的字段名称修改):
custom: function () {
var shouldBeRequired = this.field('isApproved').value == 1;
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";
}
}
}
无论 isApproved 是否为真,该字段都是可选的。
我也尝试了来自this 答案的代码,但是当它所依赖的字段 (isApproved) 更新时,optional 的值不会改变。
如何让optional 的值与另一个布尔类型字段相反?!
【问题讨论】:
标签: meteor meteor-autoform simple-schema