Dynamic Forms
This is the official documentation of the @forestadmin/agent
Node.js agent.
Business logic often requires your forms to adapt to their context. Forest Admin makes this possible through a powerful way to extend your form's logic.
To make an action form dynamic, simply use functions instead of a static value on the compatible properties.
Note that:
Both synchronous and asynchronous functions are supported
The functions take the same context object as the
execute
handler.As such, they have access to the current values of the form.
And the records that the action will be applied to.
Form field properties
functions
can be used for the following properties which are also available as static values:
Property | Description |
---|---|
| Make the field required. |
| Make the field read-only. |
| Set the default value of the field. |
| Set the text displayed below the label |
In addition, depending on the field type, you can also use functions for the following properties:
Property | Description |
---|---|
| Change the list of possible values of the field when |
| Change the target collection of the field when |
Some widgets also support dynamic configuration:
Property | Widget | Description |
---|---|---|
| Change the list of possible values of the field. | |
| Define the min value that can be entered by the user. | |
| Define the max value that can be entered by the user. | |
| Define the step value of the input. |
And finally, those two extra properties are available and can only be used as functions:
Property | Description |
---|---|
| Only display the field if the function returns true. |
| Set the current value of the field. |
Examples
Example 1: Dynamic fields based on form values
In this example we make a field required only if the user enters a value greater than 1000 in another field.
Example 2: Conditional field display based on record data
Unlike the previous example, this one will only display the field if the record has a specific value.
It is still a dynamic field, but this time, the condition does not depend on the form values but on the record data.
Example 3: Conditional enum values based on both record data and form values
You can mix and match the previous examples to create complex forms.
In this example, the form will display a different set of enum values depending on both the record data and the value of the form field.
The first field displays different denominations that can be used to address the customer, depending on the full name and gender of the customer.
The second field displays different levels of loudness depending on if the customer is Morgan Freeman, as to ensure that we never speak Very Loudly
at him, for the sake of politeness.
Example 4: Using hasFieldChanged to reset value
In this example we reset a field based on change on another one.
Example 5: Setting up default value based on the record
In this example we setup a the default value of a field based on the current record.
Example 6: Block field edition
In this example we make a field read only based on a field from the current record.
Last updated