Conditions for fields in field arrays #1426
-
Hello everyone, today I encountered a problem for which there might already be a solution, but I might not be able to find it. Snippet: {
"name":"list",
"label":"List",
"component":"object-list", //our name for a field-array
"fields":[
{
"name":"status",
"label":"Status",
"component":"select",
"clearedValue":null,
"dataType":"integer",
"options":[
{
"label":"Status 1",
"value":1
},
{
"label":"Status 2",
"value":2
},
{
"label":"Status 3",
"value":3
}
],
},
{
"name":"test",
"label":"Test",
"component":"radio",
"dataType":"boolean",
"options":[
{
"label":"No",
"value":false
},
{
"label":"Yes",
"value":true
}
],
"condition":{
"or":[
{
"when":"list[0].status", //just to highlight this
"is":1
},
{
"when":"list[0].status", //just to highlight this
"is":3
}
],
"then":{
"visible":true
}
}
}
]
} As you can see here, I have list[0].status in the "when" field as a field name. Now, logically, the condition only works for the first item in the array list. Is there a way to pass this differently in the schema, for example, list[index].status or something similar? If not, wouldn't that be a useful addition? Thanks in advance and best regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Hi @yldshv. You can define the condition as a function to be able to dynamically resolve the https://www.data-driven-forms.org/schema/condition-schema#when For your example I believe the function would look something similar to this: condition: {
or: [
{
when: (field) => {
return `${field.name.split(".").shift()}.status`;
}, //dynamic when condition
is: 1,
},
{
when: "list[0].status", //static
is: 3,
},
],
then: {
visible: true,
},
}, If you need a schema that is strictly defined via json, we have recently added a condition mapper: https://www.data-driven-forms.org/mappers/condition-mapper#heading-conditionmapper Edit: Here is a codesandbox with your example. I just had to use the default field array component. |
Beta Was this translation helpful? Give feedback.
-
Hi @Hyperkid123, |
Beta Was this translation helpful? Give feedback.
OK, you can fix the by adding a values subscription. Your condition depends on a value from another field but the form is by default set to not listen only to field changes.
Can you add
subscription={{ values: true }}
prop to the FormRendered?