Reputation: 1
I tried to create new form where there is several boolean switch button, some button will be follow another button value if change
I tried to make a form with several switch, for example there is 2 switch
switch A and B
if switch A is true, switch B will be enable and user can change value, if switch A is false, switch B will be disabled and value will change to false automatically
I tried to use "allOf" to do this task but it only can change the disable/enable switch here what I tried
{
"type": "object",
"title": "switchMode",
"properties": {
"switchA":{
"type": "boolean",
"title": "Swith A",
"description" : "if switch A is true, switch B will be enable and user can change value, if switch A is false, switch B will be disabled and value will change to false",
"default": true
},
"switchB":{
"type": "boolean",
"title": "Swith B",
"description" : "switch B value and enable/disable is depend on switch A current value",
"default": false
}
},
"allOf": [
{
"if": {
"properties": {
"switchA": {
"type": "boolean",
"const": false
}
}
},
"then": {
"properties": {
"switchB": {
"type": "boolean",
"readOnly": true
}
}
}
}
]
}
I tried to use several things but it's not work like adding some value after
"readOnly": true
with this:
"const": false
,"value": false
, "default": false
, and "enum": [ false ]
is there a way to change the switch value of switchB
into false
after change value of switchA
to false
?
Upvotes: 0
Views: 23
Reputation: 24399
It sounds like you're using a tool that generates a form from a JSON Schema. JSON Schema wasn't designed for this use, so the behavior isn't standardized. You'll need to share the exact tool your using to get definite answers. My answer is how I (a JSON Schema expert) would expect a form builder to behave, but may not necessarily be how the tool your using behaves.
You want "switchB" to only be displayed to the user if "switchA" is true. I expect that "switchB" is always displayed because it's defined a the top level of the schema. If you remove it from the top level and define it only in the then
schema, then I would expect it only to show if the if
schema passes.
With that approach, I would expect that the JSON result from that form only include "switchB" if "switchA" is true
. When "switchA" is off you would get { "swtichA": false }
when you'd prefer it to be { "swtichA": false, "switchB": false }
.
A possible solution would be to leave "switchB" defined at the top of the schema, but construct the if
/then
to disallow "swtichB".
{
"if": {
"properties": {
"switchA": { "const": false }
},
"required": ["switchA"]
},
"then": {
"not": { "required": ["switchB"] }
}
}
I think it's very unlikely that your tooling will understand this, but it might be worth a try.
is there a way to change the switch value of
switchB
intofalse
after change value ofswitchA
tofalse
?
Keep in mind that JSON Schema can't set values, it only validates existing values. So, no, there's no way to change a value under any circumstance.
Upvotes: 0
Reputation: 5333
Cross-property dependencies are imo an indication of some suboptimal earlier decision. While I did not work with jsonschema (but other XML schema definitions), the general idea of schema definitions is to remain on high level, which I outline here:
You you either have a scenario-a structure, or a scenario-b structure, and in one of them the flag is mandatory, while in the other it is absent. Resist the temptation, to require it with a fixed value instead, since this would fall on your feet with later changes to the schema needing numerous modifications.
The value of the flag should be set accordingly in the code processing the json data, taking the given value in scenario-a and unconditionally in scenario-b, preferrably in exactly one place.
Upvotes: 0