devkram
February 20, 2025, 1:26pm
1
I’ve been trying to find a way to do a conditional required field using JSONForm v3.2.1. Tried “if-then-else”, “anyOf” and “allOf” it doesn’t seems to be applying it.
Example:
{
"properties:{
"opts": {
"type": "string",
"oneOf": [{ "title": "Yes", "const": true }, { "title": "No", "const": false }]
},
"field1": { "type": "string" } // required when opts=No
}
}
sdirix
(Stefan Dirix)
February 20, 2025, 2:56pm
2
Hi @devkram ,
This is the JSON Schema you want:
{
"type": "object",
"properties": {
"opts": {
"type": "boolean",
"oneOf": [
{ "title": "Yes", "const": true },
{ "title": "No", "const": false }
]
},
"field1": {
"type": "string"
}
},
"required": ["opts"],
"if": {
"properties": { "opts": { "const": false } }
},
"then": {
"required": ["field1"]
}
}
The dynamic require validation will work and show in the UI, however we do not support the required
flag for dynamic requires. See here for more information Any way to designate if a conditionally-required field is required?