我有一个具有两个字段的SimpleSchema实例:
isApproved: {
type: Boolean,
defaultValue: false
},
impactedSystems: {
type: String,
optional: true
}
如果 isApproved 设置为optional
,我想将 impactedSystems 的false
属性更改为true
。 / p>
我尝试按照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答案中的代码,但是更新optional
依赖的字段(isApproved
)时optional
的值没有改变。
如何让{{1}}的值成为另一个布尔类型字段的对立物?!
答案 0 :(得分:1)
尝试此代码。公认有点令人费解...
这是可以在所有架构中使用的通用帮助程序:
// This helper method does the ceremony around SimpleSchema's requirements
export const isRequired = (thing,shouldBeRequired) => {
if (shouldBeRequired) {
// inserts
if (!thing.operator) {
if (!thing.isSet || thing.value === null || thing.value === "") return "required";
}
// updates
else if (thing.isSet) {
if (thing.operator === "$set" && thing.value === null || thing.value === "") return "required";
if (thing.operator === "$unset") return "required";
if (thing.operator === "$rename") return "required";
}
}
};
您可以在模式本身中执行以下操作:
const isRequiredWhenApproved = (record) => {
const shouldBeRequired = (record.field('isApproved').value);
return isRequired(record, shouldBeRequired);
};
isApproved: {
type: Boolean,
defaultValue: false
},
impactedSystems: {
type: String,
optional: true,
custom() {
return isRequiredWhenApproved(this);
},
},
我希望对您有用
答案 1 :(得分:0)
我终于明白了。我从一种形式插入 isApproved ,然后以另一种形式更新 impactedSystems 。我要做的就是在更新表单中包含 isApproved (带有type=hidden
),我希望读取其值。我提供的代码是正确的。
示例
{#autoForm collection=Requests doc=this id="singleRequestLayout" type="update"}} //This can also be of type insert
{{> afQuickField name="isApproved" type="hidden"}}
{{> afQuickField name="impactedSystems"}}
<button type="submit" class="button">Submit</button>
{{/autoForm}}