# 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.9/core/forms/#configuration-file)  
To use forms, you also need to include the `FormPolicy` in your policy configuration file. For example:

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

## [Form Basics](https://legacy-docs-v1.rasa.com/1.10.9/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.

```
## 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. As shown in the section [Handling unhappy paths](https://legacy-docs-v1.rasa.com/1.10.9/core/forms/#section-unhappy) the bot can execute any kind of actions outside the form while the form is still active. On the “happy path”, where the user is cooperating well and the system understands the user input correctly, the form is filling all requested slots without interruption.

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.

### Slot Usage
Note that for this story to work, your slots should be [unfeaturized](https://legacy-docs-v1.rasa.com/1.10.9/core/slots/#unfeaturized-slot). If any of these slots are featurized, your story needs to include `slot{}` events to show these slots being set. In that case, the easiest way to create valid stories is to use [Interactive Learning](https://legacy-docs-v1.rasa.com/1.10.9/core/interactive-learning/#interactive-learning).

### Required Methods
You need to define three methods:
- `name`: the name of this action
- `required_slots`: a list of slots that need to be filled for the `submit` method to work.
- `submit`: what to do at the end of the form, when all the slots have been filled.

```python
def name(self) -> Text:
    return "restaurant_form"
```

```python
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
    return ["cuisine", "num_people", "outdoor_seating", "preferences", "feedback"]
```

```python
def submit(
    self,
    dispatcher: CollectingDispatcher,
    tracker: Tracker,
    domain: Dict[Text, Any],
) -> List[Dict]:
    dispatcher.utter_message(template="utter_submit")
    return []
```

## [Custom slot mappings](https://legacy-docs-v1.rasa.com/1.10.9/core/forms/#id4)  
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.

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.9/core/forms/#id5)  
After extracting a slot value from user input, the form will try to validate the value of the slot. By default, validation only checks if the requested slot was successfully extracted from the slot mappings.

Here is an example validation function:

```python
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.9/core/forms/#id6)  
Of course your users will not always respond with the information you ask of them. Typically, to handle these situations, use the `action_deactivate_form` which will deactivate the form and reset the requested slot.

```
## chitchat
* request_restaurant
    - restaurant_form
    - form{"name": "restaurant_form"}
* stop
    - utter_ask_continue
* deny
    - action_deactivate_form
    - form{"name": null}
```

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