> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Forms

> Create structured data collection forms for workflow triggers via Slack

## Overview

Forms enable structured data collection that can trigger [workflows](https://docs.formal.ai/docs/guides/configuration/workflows.md). A form defines a set of typed fields that users fill out via Slack. When submitted, the form data is available to workflow actions via CEL expressions.

Forms are useful for **Access requests**: Enable users to request access to perform specific actions on resources with time-bound parameters for policy suspensions.

## Form Structure

A form consists of:

* **Name**: A unique name within your organization
* **Description**: Optional description shown to users when filling out the form
* **Fields**: One or more typed fields for data collection

### Field Types

**Text and numeric inputs:**

* `string` — Plain text input
* `number` — Numeric input
* `email` — Email address input
* `url` — URL input

**Date and time inputs:**

* `date` — Date picker
* `time` — Time picker
* `timestamp` — Combined date and time picker

**Selection inputs** (require options configuration — see [below](#selection-field-options)):

* `select` — Single-select dropdown
* `multi_select` — Multi-select dropdown
* `checkbox` — Checkbox group
* `radio` — Radio button group

### Field IDs

Each field has an `id` that must match the pattern `field_[_a-zA-Z0-9]+`. This ID is used to reference field values in workflow actions via `trigger.form_submission.submission.field_<id>`, and in validation rules and conditions.

### Optional Fields

Fields are required by default. Set `optional` to let users leave a field empty. An unanswered optional field is submitted as `""`, or `[]` for `multi_select` and `checkbox`, so workflow expressions that reference it still resolve.

### Validation Rules

A field can have a validation rule: a [CEL](https://cel.dev) expression that must evaluate to `true` when the form is submitted, and an optional error message (up to 100 characters). Fields are variables named by their ID, with the values workflows receive.

| Rule | Checks that |
| - | - |
| `field_ticket.matches("^INC-[0-9]+$")` | The ticket looks like `INC-1234` |
| `size(field_reason) >= 20` | The reason has at least 20 characters |
| `field_hours <= field_max_hours` | One answer does not exceed another |

In Slack, a failing rule shows its message under the field and the form stays open, so the user can fix the answer without starting over. A rule that errors also fails. A rule on an optional field must accept the empty answer, for example `field_hours == "" || field_hours <= 24`.

### Conditional Fields and Options

A field, or a static option of a selection field, can have a `visible_if` condition: a CEL expression over the answers to **earlier** `select`, `radio`, `multi_select`, and `checkbox` fields. The field or option is shown only when the condition is `true`.

| Condition | Shows the field or option when |
| - | - |
| `field_env == "prod"` | Production is picked |
| `field_resource.startsWith("prod-")` | The picked value starts with `prod-` |
| `"pii" in field_data` | PII is among the ticked boxes |
| `size(field_data) > 0` | At least one box is ticked |

* Hidden fields are left empty and their rules are skipped.
* If all of a field's options are hidden, the field is hidden too.
* Only options currently shown can be submitted.
* If a condition breaks, the form can't be submitted until an admin fixes it.

Conditions are checked when the form is saved, so a condition can only reference earlier selection fields and must evaluate to a bool.

In the form builder, each field has a **Rules & conditions** section for the optional switch, the validation rule, and the condition, and each static option can have its own condition. The preview applies conditions as you pick answers.

#### Example

An access request where the duration and ticket depend on the environment, and sensitive data needs a justification:

```json theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
[
  {
    "id": "field_env", "name": "Environment", "type": "radio",
    "config": { "options": [
      { "label": "Development", "value": "dev" },
      { "label": "Production", "value": "prod" }
    ]}
  },
  {
    "id": "field_duration", "name": "Duration", "type": "select",
    "config": { "options": [
      { "label": "1 hour", "value": "1h" },
      { "label": "8 hours", "value": "8h" },
      { "label": "24 hours", "value": "24h", "visible_if": "field_env == \"dev\"" }
    ]}
  },
  {
    "id": "field_ticket", "name": "Incident ticket", "type": "string",
    "visible_if": "field_env == \"prod\"",
    "validation": {
      "rule": "field_ticket.matches(\"^INC-[0-9]+$\")",
      "message": "Use an incident ticket like INC-1234"
    }
  },
  {
    "id": "field_data", "name": "Data accessed", "type": "checkbox", "optional": true,
    "config": { "options": [
      { "label": "Contains PII", "value": "pii" },
      { "label": "Contains financial data", "value": "financial" }
    ]}
  },
  {
    "id": "field_justification", "name": "Why do you need sensitive data?", "type": "string",
    "visible_if": "size(field_data) > 0",
    "validation": {
      "rule": "size(field_justification) >= 20",
      "message": "Give at least 20 characters of context"
    }
  }
]
```

### Selection Field Options

The `select`, `multi_select`, `checkbox`, and `radio` field types all require an options configuration: either **static options** or a **dynamic options source**, not both.

#### Static Options

Provide a fixed list of options directly in the field configuration. Each option has a `label` (displayed to the user) and a `value` (submitted with the form).

#### Dynamic Options

Dynamic sources populate a selection field by calling a `List` endpoint on Formal's API. This is useful when the set of options changes over time (e.g., listing resources, users, or datastores).

A dynamic source has the following configuration:

| Parameter | Description |
| - | - |
| **App** | The service name (e.g., `core.v1.ResourceService`) |
| **Command Name** | The method name without the `List` prefix (e.g., `Resources` for the `ListResources` method) |
| **Machine User ID** | The machine user used to authenticate options retrieval |
| **Input** | The JSON request body. Use `${{ query }}` to interpolate the user's search text, and `${{ field_<id> }}` to interpolate the answer to an earlier field (see [below](#options-that-depend-on-an-earlier-answer)). You can also pass `searchFields` to control which fields are searched |
| **Transform** | A CEL expression evaluated against the response body (available via the `body` variable) when the response is a 200. Should return a list of objects with `value` and `text` fields |

The `searchFields` parameter in the input controls which fields the `search` term is matched against. For example, `"searchFields": ["name", "technology", "hostname"]` restricts matching to those three fields on the target resource.

#### Options That Depend on an Earlier Answer

`${{ field_<id> }}` in the input is replaced with the answer to that field, so the options follow an earlier choice. For example, to list only the resources of the technology the user picked:

```json theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
{
  "search": "${{ field_technology }}",
  "searchFields": ["technology"]
}
```

* The referenced field must be an earlier `select` or `radio` field with static or dynamic options.
* The field stays hidden until every referenced field is answered, so an empty filter never lists everything.
* Changing an answer clears the selections that depend on it.
* Options load as the source's machine user, whatever the earlier answers are. Grant it only what every filler may see.

##### Example: Space → Resource → Native User

Add three required `select` fields in this order:

| Field ID | App | Command name | Input | Transform |
| - | - | - | - | - |
| `field_space` | `Space` | `Spaces` | `{"limit":100,"search":"${{ query }}","searchFields":["name"]}` | `body.spaces.map(s, {"value": s.id, "text": s.name})` |
| `field_resource` | `Resource` | `Resources` | `{"limit":100,"spaceId":"${{ field_space }}","search":"${{ query }}","searchFields":["name"]}` | `body.resources.map(r, {"value": r.id, "text": r.name})` |
| `field_native_user` | `NativeUser` | `NativeUsersV3` | `{"limit":100,"resourceId":"${{ field_resource }}"}` | `body.nativeUsers.map(u, {"value": u.label, "text": u.label})` |

## Filling Out Forms

### Slack Integration

Forms can be filled out via Slack using the "Fill out form" shortcut. A [Slack integration](https://docs.formal.ai/docs/guides/integrations/chats.md) needs to be linked to your Formal organization.

#### Slash Command

Type `/formal form` to open a modal where you can select and fill out a form.

#### Submission Flow

1. User opens the form shortcut in Slack
2. User selects a form from the searchable dropdown
3. User fills out the fields. Picking a choice updates the form, showing or hiding fields and options that depend on it
4. User submits. Answers that fail a validation rule, or options no longer offered, are flagged under their field and the form stays open
5. A confirmation message with the submitted values is sent to the user
6. Any workflows with a matching `form-submission` trigger are executed

### Formal Endpoint

Forms can also be filled out via the Formal Endpoint.

#### Submission Flow

1. User opens the Formal Endpoint
2. User runs `formal form fill` and selects a form from the shown list
3. User fills out the fields and submits
4. A confirmation message with the submitted values is shown to the user, along with a command to submit the same payload again without needing to re-select the form
5. Any workflows with a matching `form-submission` trigger are executed

You can also display forms and their fields using `formal form list -o [jsonl|yaml]` for programmatic use.

Submissions from the Endpoint go through the same server-side checks as Slack: required fields, offered options, conditions, and validation rules.

## Using Forms in Workflows

Forms connect to workflows via the `form-submission` trigger type. See the [workflows documentation](https://docs.formal.ai/docs/guides/configuration/workflows.md#form-submission) for trigger details.

Form data will not be saved in the Formal Control Plane unless there is at least one workflow with the corresponding `form-submission` trigger type.

### Example: Access Request with Approval

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  type: form-submission
  name: access_request
  args:
    id: form_abc123

actions:
  - type: ask-in-chat
    name: ask_approval
    args:
      message: "${{ trigger.form_submission.submitter_email }} is requesting access to ${{ trigger.form_submission.submission.field_resource }}. Reason: ${{ trigger.form_submission.submission.field_reason }}. Approve?"
      recipient_channel: "security-approvals"
      integration: slack
```

### Referencing Form Data in CEL

Form submission data is available under `trigger.form_submission`:

| Expression | Description |
| - | - |
| `trigger.form_submission.form_id` | The ID of the submitted form |
| `trigger.form_submission.form_name` | The name of the submitted form |
| `trigger.form_submission.submitter_id` | Formal user ID of the submitter; empty if a Slack submitter has no matching Formal user |
| `trigger.form_submission.submitter_email` | Email of the user in Slack who submitted the form |
| `trigger.form_submission.submission.field_<id>` | Value of a specific field by its ID |

### Scope a Suspension to the Selected Native User

In the [approval workflow's suspension action](https://docs.formal.ai/docs/guides/configuration/workflows.md#terraform-examples), set `args.input.input_condition` to:

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
input_condition: >-
  "%x".format([input.resource.id]) == "${{ "%x".format([trigger.form_submission.submission.field_resource]) }}"
  && "%x".format([input.native_user]) == "${{ "%x".format([trigger.form_submission.submission.field_native_user]) }}"
```

`input.native_user` is the native user's label, not its ID. In a Terraform heredoc, write `$${{ … }}`.

## Terraform Example

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_form" "access_request" {
  name        = "access-request-form"
  description = "Form for requesting database access"

  field {
    id   = "field_reason"
    name = "Reason"
    type = "string"
  }

  field {
    id   = "field_resource"
    name = "Resource"
    type = "select"

    config {
      option {
        label = "Production"
        value = "production"
      }
      option {
        label = "Staging"
        value = "staging"
      }
    }
  }

  field {
    id   = "field_duration"
    name = "Duration (hours)"
    type = "number"
  }
}
```

For a dynamic options source (e.g., fetching resources from Formal):

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_form" "dynamic_access_request" {
  name        = "dynamic-access-request"
  description = "Access request with dynamic resource selection"

  field {
    id   = "field_resource"
    name = "Resource"
    type = "select"

    config {
      options_source {
        app             = "core.v1.ResourceService"
        machine_user_id = formal_user.machine.id
        transform       = "body.resources.map(r, {\"value\": r.id, \"text\": r.name})"

        command {
          name = "Resources"
        }

        input = {
          search       = "${{ query }}"
          limit        = "25"
          searchFields = jsonencode(["name", "technology", "hostname"])
        }
      }
    }
  }

  field {
    id   = "field_reason"
    name = "Reason"
    type = "string"
  }
}
```

See the [formal\_form resource documentation](https://registry.terraform.io/providers/formalco/formal/latest/docs/resources/form) for the full schema reference.

## Next Steps

> [card] [Workflows](https://docs.formal.ai/docs/guides/configuration/workflows.md)
>
> Learn about workflow triggers, actions, and chaining

> [card] [Slack Integration](https://docs.formal.ai/docs/guides/integrations/chats.md)
>
> Configure your Slack integration for forms


## Related topics

- [Get form](https://docs.formal.ai/api-reference/corev1workflowservice/get-form.md)
- [List forms](https://docs.formal.ai/api-reference/corev1workflowservice/list-forms.md)
- [Create form](https://docs.formal.ai/api-reference/corev1workflowservice/create-form.md)
- [Update form](https://docs.formal.ai/api-reference/corev1workflowservice/update-form.md)
- [Delete form](https://docs.formal.ai/api-reference/corev1workflowservice/delete-form.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.