Dynamic Forms

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

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:

PropertyDescription

is_required

Make the field required.

is_read_only

Make the field read-only.

default_value

Set the default value of the field.

In addition, depending on the field type, you can also use functions for the following properties:

PropertyDescription

enum_values

Change the list of possible values of the field when type == 'Enum'.

collection_name

Change the target collection of the field when type: 'Collection'.

And finally, those two extra properties are available and can only be used as functions:

PropertyDescription

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.

from typing import Union
from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> Union[None, ActionResult]:
    pass

agent.customize_collection("customer").add_action("Charge credit card", {
    "form": [
        {
          "label": 'amount',
          "type": "Number",
        },
        {
          "label": 'description',
          "type": "String",
          # The field will only be required if the function returns true.
          "is_required": lambda context: context.form_values.get('amount', 0) > 1000,

        },
    ],
    "scope": "Single",
    "execute": execute,
})

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.

from typing import Union
from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> Union[None, ActionResult]:
    # perform work here

agent.customize_collection("Product").add_action("Leave a review", {
    "form": [
        {
            "label": "Rating",
            "type": "Enum",
            "enum_values": ["1", "2", "3", "4", "5"]
        },
        {
            "label": "Put a comment",
            "type": "String",
            # Only display this field if the rating is 4 or 5
            "if_": lambda context: int(context.form_values.get("Rating", "0")) >= 4,
        },
    ],
})

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.

from typing import Union
from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

def get_loudly_enum_values(context: ActionContextSingle):
    # Enum values are computed based on the record data
    # Because we need to fetch the record data, we need to use an async function
    denomination = context.form_values.get("How should we refer to you?")
    if denomination == "Morgan Freeman":
      return ['Whispering', 'Softly', 'Loudly']
    else:
      return ['Softly', 'Loudly', 'Very Loudly']

def get_denomination_enum_values(context: ActionContextSingle):
    # Enum values are computed based on another form field value
    # (no need to use an async function here, but doing so would not be a problem)
    person = context.get_record(['firstName', 'lastName', 'gender'])
    base = [person['firstName'], f"{person['firstName']} {person['lastName']}"]
    if (person['gender'] == 'Female'):
      base.append(f"Mrs.  {person['lastName']}")
      base.append(f"Miss.  {person['lastName']}")
    else:
      base.append(f"Mr.  {person['lastName']}")
    return base

async def execute(
    context: ActionContextSingle, result_builder: ResultBuilder
) -> Union[None, ActionResult]:
    denomination = context.form_values["How should we refer to you?"]
    loudness = context.form_values["How loud should we say it?"]

    text = f"Hello {denomination}"
    if loudness == 'Whispering':
      text = text.lower()
    elif loudness == 'Loudly':
      text = text.upper()
    elif loudness == 'Very Loudly:
      text = f"{text.upper()}!!!"

    return result_builder.success(text)

agent.customize_collection("customer").add_action("Tell me a greeting", {
    "form": [
        {
            "label": "How should we refer to you?",
            "type": "Enum",
            "enum_values": get_denomination_enum_values
        }
        {
            "label": "How should we refer to you?",
            "type": "Enum",
            "enum_values": get_loudly_enum_values
        }
    ],
    "scope": "Single",
    "execute": execute,
})

Example 4: Using has_field_changed to reset value

In this example we reset a field based on change on another one.

from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

def bank_name_value(context: ActionContextSingle):
    if context.has_field_changed('Bank Name'):
        if context.form_value["Bank Name"] == "CE":
            return "CEPAFRPPXXX"
        else:
            return "CCBPFRPPXXX"

agent.customize_collection("customer").add_action("Create banking identity", {
    "form": [
        {
            "label": "Bank Name",
            "type": "Enum",
            "is_required": True,
            "enum_values":  ['CE', 'BP'],
        },
        {
            "label": "BIC",
            "type": "String",
            "value": bank_name_value,
        }
    ],
    "scope": "Single",
    "execute": lambda context, result_builder: pass,
})

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.

from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def new_price_default_value(context: ActionContextSingle):
    return await context.get_record(['amount'])['amount']

agent.customize_collection("customer").add_action("Change order price", {
    "form": [
        {
            "label": 'New Price',
            "type": "Number",
            "default_value": new_price_default_value,
        }
    ],
    "scope": "Single",
    "execute": lambda context, result_builder: #...
})

Example 6: Block field edition

In this example we make a field read only based on a field from the current record.

from forestadmin.datasource_toolkit.decorators.action.context.single import ActionContextSingle
from forestadmin.datasource_toolkit.decorators.action.result_builder import ResultBuilder
from forestadmin.datasource_toolkit.interfaces.actions import ActionResult

async def new_price_default_value(context: ActionContextSingle):
    return await context.get_record(['amount'])['amount']

async def new_price_is_read_only(context: ActionContextSingle):
    return await context.get_record(['status'])['status'] == 'paid'

agent.customize_collection("customer").add_action("Change order price", {
    "form":  [
        {
            "label": 'New Price',
            "type": "Number",
            "default_value": new_price_default_value,
            "is_read_only": new_price_is_read_only
        }
    ],
    "scope": "Single",
    "execute": lambda context, result_builder: #...
})

Last updated