# Forms

Note

There is an in-depth tutorial [here](https://blog.rasa.com/building-contextual-assistants-with-rasa-formaction/) about how to use Rasa Forms for slot filling.

## [Configuration File](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#configuration-file)
To use forms, you also need to include the `FormPolicy` in your policy configuration file. For example:

```yaml
policies:
  - name: "FormPolicy"
```

## [Form Basics](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#form-basics)
Using a `FormAction`, you can describe _all_ of the happy paths with a single story. By “happy path”, we mean that whenever you ask a user for some information, they respond with the information you asked for.

```yaml
## happy path
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
    - form{"name": null}
```

In this story the user intent is `request_restaurant`, which is followed by the form action `restaurant_form`. With `form{"name": "restaurant_form"}` the form is activated and with `form{"name": null}` the form is deactivated again.

The `FormAction` will only request slots which haven’t already been set. If a user starts the conversation with I’d like a vegetarian Chinese restaurant for 8 people, then they won’t be asked about the `cuisine` and `num_people` slots.

## [Custom slot mappings](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#custom-slot-mappings)
If you do not define slot mappings, slots will be only filled by entities with the same name as the slot that are picked up from the user input. Some slots, like `cuisine`, can be picked up using a single entity, but a `FormAction` can also support yes/no questions and free-text input.

Here’s an example for the restaurant bot:

```python
def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict]]]:
    return {
        "cuisine": self.from_entity(entity="cuisine", not_intent="chitchat"),
        "num_people": [
            self.from_entity(
                entity="number", intent=["inform", "request_restaurant"]
            ),
        ],
        "outdoor_seating": [
            self.from_entity(entity="seating"),
            self.from_intent(intent="affirm", value=True),
            self.from_intent(intent="deny", value=False),
        ],
        "preferences": [
            self.from_intent(intent="deny", value="no additional preferences"),
            self.from_text(not_intent="affirm"),
        ],
        "feedback": [self.from_entity(entity="feedback"), self.from_text()],
    }
```

## [Validating user input](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#validating-user-input)
After extracting a slot value from user input, the form will try to validate the value of the slot. Note that by default, validation only happens if the form action is executed immediately after user input. This can be changed in the `_validate_if_required()` function of the `FormAction` class in Rasa SDK.

Here is an example , `validate_cuisine()`, which checks if the extracted cuisine slot belongs to a list of supported cuisines.

```python
@staticmethod
def cuisine_db() -> List[Text]:
    return [
        "caribbean",
        "chinese",
        "french",
        "greek",
        "indian",
        "italian",
        "mexican",
    ]

def validate_cuisine(self, value: Text, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]:
    if value.lower() in self.cuisine_db():
        return {"cuisine": value}
    else:
        dispatcher.utter_message(template="utter_wrong_cuisine")
        return {"cuisine": None}
```

## [Handling unhappy paths](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#handling-unhappy-paths)
Of course your users will not always respond with the information you ask of them. Typically, users will ask questions, make chitchat, change their mind, or otherwise stray from the happy path. The way this works with forms is that a form will raise an `ActionExecutionRejection` if the user didn’t provide the requested information.

## [The requested_slot slot](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#the-requested-slot-slot)
The slot `requested_slot` is automatically added to the domain as an unfeaturized slot. If you want to make it featurized, you need to add it to your domain file as a categorical slot.

## [Debugging](https://legacy-docs-v1.rasa.com/1.10.2/core/forms/#debugging)
The first thing to try is to run your bot with the `debug` flag.
