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.
But you can also define entirely your form to be dynamic.
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:
isRequired
Make the field required.
isReadOnly
Make the field read-only.
defaultValue
Set the default value of the field.
description
Set the text displayed below the label
In addition, depending on the field type, you can also use functions for the following properties:
enumValues
Change the list of possible values of the field when type == 'Enum'
.
collectionName
Change the target collection of the field when type: 'Collection'
.
Some widgets also support dynamic configuration:
options
Change the list of possible values of the field.
min
Define the min value that can be entered by the user.
max
Define the max value that can be entered by the user.
step
Define the step value of the input.
And finally, those two extra properties are available and can only be used as functions:
if
Only display the field if the function returns true.
value
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.
Exemple 7: Define a totally dynamic form
In this example we define the fields to be all the fields of the current collection. Doing that will allow the form to be always up to date and to be able to enforce the edition of a record through this action for example.
Last updated