Update Data Entry Flow docs with field pre-fill (#492)

Update the docs with instructions on pre-filling values using either `default` or `suggested_value`, and the differences between them.
This commit is contained in:
On Freund 2020-05-03 16:26:22 +03:00 committed by GitHub
parent ff435a269f
commit f0973bf4d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -107,7 +107,7 @@ class ExampleConfigFlow(data_entry_flow.FlowHandler):
### Show Form ### Show Form
This result type will show a form to the user to fill in. You define the current step, the schema of the data (using voluptuous) and optionally a dictionary of errors. Title and description of the step will be provided via the translation file. Where this is defined depends on the context of the data entry flow. This result type will show a form to the user to fill in. You define the current step, the schema of the data (using voluptuous) and optionally a dictionary of errors.
```python ```python
class ExampleConfigFlow(data_entry_flow.FlowHandler): class ExampleConfigFlow(data_entry_flow.FlowHandler):
@ -124,6 +124,28 @@ class ExampleConfigFlow(data_entry_flow.FlowHandler):
return self.async_show_form(step_id="init", data_schema=vol.Schema(data_schema)) return self.async_show_form(step_id="init", data_schema=vol.Schema(data_schema))
``` ```
If you'd like to pre-fill data in the form, you have two options. The first is to use the `deafult` parameter. This will both pre-fill the field, and act as the default value in case the user leaves the field empty.
```python
data_schema = {
vol.Optional("field_name", default="default value"): str,
}
```
The other alternative is to use a suggested value - this will also pre-fill the form field, but will allow the user to leave it empty if the user so wishes.
```python
data_schema = {
vol.Optional(
"field_name", description={"suggested_value": "suggested value"}
): str,
}
```
You can also mix and match - pre-fill through `suggested_value`, and use a different value for `default` in case the field is left empty, but that could be confusing to the user so use carefully.
Title and description of the step will be provided via the translation file. Where this is defined depends on the context of the data entry flow.
After the user has filled in the form, the step method will be called again and the user input is passed in. Your step will only be called if the user input passes your data schema. When the user passes in data, you will have to do extra validation of the data. For example, you can verify that the passed in username and password are valid. After the user has filled in the form, the step method will be called again and the user input is passed in. Your step will only be called if the user input passes your data schema. When the user passes in data, you will have to do extra validation of the data. For example, you can verify that the passed in username and password are valid.
If something is wrong, you can return a dictionary with errors. Each key in the error dictionary refers to a field name that contains the error. Use the key `base` if you want to show an error unrelated to a specific field. The specified errors need to refer to a key in a translation file. If something is wrong, you can return a dictionary with errors. Each key in the error dictionary refers to a field name that contains the error. Use the key `base` if you want to show an error unrelated to a specific field. The specified errors need to refer to a key in a translation file.