I have a SimpleSchema instance with two fields:
isApproved: {
type: Boolean,
defaultValue: false
},
impactedSystems: {
type: String,
optional: true
}
I'd like to change the optional
attribute of impactedSystems to false
if isApproved is set to true
.
I've tried following the instructions in the docs but I can't get it to work. It suggests I add a custom function to impactedSystems like so (modified with my field names):
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";
}
}
}
The field stays optional whether isApproved is true or not.
I also tried the code from this answer but the value of optional
doesn't change when the field it depends on (isApproved
) is updated.
How can I have the value of optional
become the opposite of another boolean type field?!
Try this code. it is admittedly a little convoluted...
This is a generic helper you can use across all your schemas:
In the schema itself you can do it like this:
I hope that works for you
I finally figured it out. I was inserting isApproved from one form and then updating impactedSystems in another. All I had to do was include isApproved (with
type=hidden
) in the update form where I want its value to be read. The code I provided is correct.Example