Use a Smart Action Form
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize and forest-express-mongoose Node.js agents that will soon reach end-of-support.
forest-express-sequelize v9 and forest-express-mongoose v9 are replaced by @forestadmin/agent v1.
Please check your agent type and version and read on or switch to the right documentation.
This is still the latest Ruby on Rails documentation of the forest_liana agent, you’re at the right place, please read on.
This is the documentation of the django-forestadmin Django agent that will soon reach end-of-support.
If you’re using a Django agent, notice that django-forestadmin v1 is replaced by forestadmin-agent-django v1.
If you’re using a Flask agent, go to the forestadmin-agent-flask v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
This is the documentation of the forestadmin/laravel-forestadmin Laravel agent that will soon reach end-of-support.
If you’re using a Laravel agent, notice that forestadmin/laravel-forestadmin v1 is replaced by forestadmin/laravel-forestadmin v3.
If you’re using a Symfony agent, go to the forestadmin/symfony-forestadmin v1 documentation.
Please check your agent type and version and read on or switch to the right documentation.
Use a Smart Action Form
We've just introduced Smart actions: they're great because you can execute virtually any business logic. However, there is one big part missing: how do you let your users provide more information or have interaction when they trigger the Smart action? In short, you need to open a Smart Action Form.
Opening a Smart Action Form
Very often, you will need to ask user inputs before triggering the logic behind a Smart Action. For example, you might want to specify a reason if you want to block a user account. Or set the amount to charge a user’s credit card.
On our Live Demo example, we’ve defined 4 input fields on the Smart Action Upload Legal Docs on the collection companies.
const { collection } = require('forest-express-sequelize');
collection('companies', {
actions: [
{
name: 'Upload Legal Docs',
type: 'single',
fields: [
{
field: 'Certificate of Incorporation',
description:
'The legal document relating to the formation of a company or corporation.',
type: 'File',
isRequired: true,
},
{
field: 'Proof of address',
description:
'(Electricity, Gas, Water, Internet, Landline & Mobile Phone Invoice / Payment Schedule) no older than 3 months of the legal representative of your company',
type: 'File',
isRequired: true,
},
{
field: 'Company bank statement',
description: 'PDF including company name as well as IBAN',
type: 'File',
isRequired: true,
},
{
field: 'Valid proof of ID',
description:
'ID card or passport if the document has been issued in the EU, EFTA, or EEA / ID card or passport + resident permit or driving license if the document has been issued outside the EU, EFTA, or EEA of the legal representative of your company',
type: 'File',
isRequired: true,
},
],
},
],
});On our Live Demo example, we’ve defined 4 input fields on the Smart Action Upload Legal Docs on the collection companies.
On our Live Demo example, we’ve defined 4 input fields on the Smart Action Upload Legal Docs on the collection Company.
On our Live Demo example, we’ve defined 4 input fields on the Smart Action Upload Legal Docs on the collection Company.
Ensure the file app/forest/__init__.py exists and contains the import of the previous defined class :
On our Live Demo example, we’ve defined 4 input fields on the Smart Action Upload Legal Docs on the collection Company.

Handling input values
Here is the list of available options to customize your input form.
field
string
Label of the input field.
type
string or array
Type of your field.
string:
Boolean,Date,Dateonly,Enum,File,Number,Stringarray:
['Enum'],['Number'],['String']
reference
string
(optional) Specify that the input is a reference to another collection. You must specify the primary key (ex: category.id).
enums
array of strings
(optional) Required only for the Enum type. This is where you list all the possible values for your input field.
description
string
(optional) Add a description for your admin users to help them fill correctly your form
isRequired
boolean
(optional) If true, your input field will be set as required in the browser. Default is false.
hook
string
(optional) Specify the change hook. If specified the corresponding hook is called when the input change
widget
string
(optional) The following widgets are available to your smart action fields (text area, date, boolean, file, dateonly)
The widget property is only partially supported. If you want to use a custom widget via a Smart Action Hook, you'll need to use the syntax mentioned in the next section.
Use components to better layout your form
you must define your layout in a load hook at minima, and repeat it in each change hook.
This feature is useful when dealing with long/complex forms, with many fields. It will let you organize them and add useful information to guide the end user. The layout must contain the fields as they should be rendered on the form.
List of supported layout components
Example
Here's an example of an action form with many fields, that we want to improve with some layout components, to make it easier for the end user to fill in.
The resulting action form will be:

Making a form dynamic with hooks
Business logic often requires your forms to adapt to its context. Forest Admin makes this possible through a powerful way to extend your form's logic.
To make Smart Action Forms dynamic, we've introduced the concept of hooks: hooks allow you to run some logic upon a specific event.
The load hook is called when the form loads, allowing you to change its properties upon load.
The change hook is called whenever you interact with a field of the form.
Prefill a form with default values
Forest Admin allows you to set default values of your form. In this example, we will prefill the form with data coming from the record itself (1), with just a few extra lines of code.

Making a field read-only
To make a field read only, you can use the isReadOnly property:
isReadOnly
boolean
(optional) If true, the Smart action field won’t be editable in the form. Default is false
Combined with the load hook feature, this can be used to make a field read-only dynamically:
Change your form's data based on previous field values
Here's a typical example: Selecting a City within a list of cities from the Country you just selected. Then selecting a Zip code within a list of zip codes located in the City you just selected.
How does it work?
The hooks property receives a context object containing:
the
fieldsarray in its current state (containing also the current values)the
requestobject containing all the information related to the records selection. Explained here.the
changedFieldis the current field who trigger the hook (only for change hook)
If you want to use a widget inside of a hook, you'll need to use the following syntax on your field:
For a
text area, use{ widgetEdit: 'text area editor', parameters: {} }For a
boolean, use{ widgetEdit: 'boolean editor', parameters: {} }For a
dateor adateonly, use{ widgetEdit: 'date editor', parameters: {} }For a
file, use{ widgetEdit: 'file picker', parameters: {} }
To dynamically change a property within a load or change hook, just set it! For instance, setting a new description for the field city:
As a result, the correct way to set a default value is using the value property within a load hook, as follows:
Add/remove fields dynamically
You can add a field dynamically inside the fields array, like so:
Note that you may add a change hook on a dynamically-added field. Simply use the following syntax:
Get selected records with bulk action
When using hooks with a bulk Smart action, you'll probably need te get the values or ids of the selected records. See below how this can be achieved.
Last updated
Was this helpful?