Form
This is the official documentation of Forest Admin Cloud.
Frequently, it is necessary to request information before proceeding with the execution of an action. In Forest Admin, this occurs through a form that pops up before the action is executed. For example, you might need to explain why you need to block a user account or decide how much to charge a user's credit card.
TL;DR
Ensure you update the collection and action names as needed.
To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.
Creating a static form
Add the form
key in the action definition to open a form before executing the action:
Arguments:
Form [Object]: A JavaScript array that contains a list of JavaScript Objects, each representing a field to add in the form.
label
* String: The name of the field.type
* String: The type of the field. Expected value:Boolean
Date
Dateonly
Enum
Json
Number
NumberList
EnumList
String
StringList
File
FileList
Collection: See Configuring a Relationship as a Form Field
collectionName
String: Required whentype
is Collection.defaultValue
any: Set the default value of the field.description
String: A description that explains the field's function and usage for clarity.enumValues
[String]: Required whentype
is Enum.isReadOnly
Boolean: Specifies whether the field is mandatory or not. Default false.isRequired
Boolean: Specifies whether the field is mandatory or not. Default false.widget
String: Customize the widget to display the field.
Creating a dynamic form
To create a form that changes based on the values of other fields, use a function instead of a static value for properties that support dynamic values. Both synchronous and asynchronous functions are supported. These functions have the same context
argument as the execute()
function.
Properties that support dynamic values:
isRequired
isReadOnly
defaultValue
description
enumValues
if type isEnum
collectionName
if type isCollection
When setting up a dynamic form, you can use two additional properties:
if
: Display the field if the condition is met.value
: Sets the field's current value.
Widgets properties that support dynamic values:
options
min
max
step
Retrieving form values
Within the execute()
function, you can access and use the form input values provided by the user who initiated the action by accessing the formValues
attribute from the context.
Examples
To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.
Configuring a Relationship as a Form Field
When you use the Collection
type, you can link to any record from any collection. This link uses the record's primary key as its value.
The value is stored in an array to handle composite primary keys. For non-composite keys, the array contains one value.
Making a field required dynamically
In this example, we request to fill the reason field if the amount of the credit card charge exceeds 1000.
Last updated