Dynamic forms are a powerful feature that allows the behavior and appearance of form elements to change based on certain conditions. These conditions are defined using JSON objects, which enable you to create complex form interactions and provide a more dynamic and user-friendly form experience.
In this documentation, we will explore different scenarios and cases of dynamic forms, including showing/hiding form elements based on conditions and dynamically setting options and other fields based on user input.
Basic Show/Hide
The simplest form of dynamic forms is to show or hide form elements based on a condition. For example:
In this example, if the "parent" checkbox is not checked, the "child" dropdown options will show list from else condition.
Setting Options with Array of Conditions
You can use array conditions to set different options based on multiple conditions:
[
{
"type": "text",
"title": "In this example, the dropdown below is controlled by text field. Dropdown options are depended on typed value",
"payloadName": "parent",
"helpText": "Type your favorite superhero universe, e.g. 'marvel' or 'dc' or something else"
},
{
"type": "dropdown",
"title": "Please type something in the text field above",
"payloadName": "child",
"condition": [
{
"when": "parent",
"is": "marvel",
"then": {
"title": "Some Marvel's hero",
"helpText": " ",
"options": [
{
"value": "iron_man",
"label": "Tony Stark"
},
{
"value": "hulk",
"label": "Bruce Banner"
}
]
}
},
{
"when": "parent",
"is": "dc",
"then": {
"title": "Some DC's hero",
"helpText": " ",
"options": [
{
"value": "batman",
"label": "Bruce Wayne"
},
{
"value": "cat-woman",
"label": "Selina Kyle"
}
]
}
}
]
}
]
In this example, if the "parent" input is set to "marvel," the "child" dropdown options will change to include Marvel characters like "Tony Stark" and "Bruce Banner." If set to "dc," the options will change to include DC characters like "Bruce Wayne" and "Selina Kyle."
Dynamic Settings based on Dropdown Selection
You can also use dynamic settings based on dropdown selections, allowing for cascading dropdowns:
In this example, the "child" dropdown options will change based on the selection made in the "parent" dropdown. If "Marvel" is selected, the options will include "Tony Stark" and "Bruce Banner." If "DC" is selected, the options will include "Bruce Wayne" and "Selina Kyle."
Other Dynamic Fields
Modifying Form Element Properties
Dynamic forms can be used to modify other properties of form elements based on certain conditions:
In this example, when the "parent" checkbox is checked, the "child" field's title will change and a help text as well as the default value will be set.
Empty and Not Empty Conditions
Dynamic forms can also be based on the presence or absence of values in form elements:
isEmpty Condition
[
{
"type": "text",
"title": "Example using 'isEmpty' keyword",
"payloadName": "parent",
"helpText": "Type something"
},
{
"type": "text",
"title": "I'm visible only when the parent field is empty",
"payloadName": "child",
"condition": {
"when": "parent",
"isEmpty": true
}
}
]
In this example, the "child" field will only be visible if the "parent" field is empty.
isNotEmpty Condition
[
{
"type": "text",
"title": "Example using 'isNotEmpty' keyword",
"payloadName": "parent",
"helpText": "Type something"
},
{
"type": "text",
"title": "I'm visible only when the parent field isn't empty",
"payloadName": "child",
"condition": {
"when": "parent",
"isNotEmpty": true
}
}
]
In this example, the "child" field will only be visible if the "parent" field is not empty.
Conclusion
Dynamic forms offer powerful capabilities to create interactive and personalized form experiences. By defining conditions and using logical operators, you can control when form elements are displayed or hidden. Additionally, you can modify form element properties and set different options based on user input, providing a more dynamic and user-friendly form interaction. Use these techniques to enhance your forms and make them more intuitive and tailored to your users' needs.