Reflex Logo
Docs Logo
Library

/

Forms

/

Form

/

Low

High Level

Low Level

Forms are used to collect information from your users. Forms group the inputs and submit them together.

Here is an example of a form collecting an email address, with built-in validation on the email. If email entered is invalid, the form cannot be submitted. Note that the form.submit button is not automatically disabled. It is still clickable, but does not submit the form data. After successful submission, an alert window shows up and the form is cleared. There are a few flex containers used in the example to control the layout of the form components.

In this example, the rx.input has an attribute type="email" and the form.message has the attribute match="typeMismatch". Those are required for the form to validate the input by its type. The prop as_child="True" is required when using other components to construct a Form component. This example has used rx.input to construct the Form Control and button the Form Submit.

A Form Root (form.root) contains all the parts of a form. The Form Field (form.field), Form Submit (form.submit), etc should all be inside a Form Root. A Form Field can contain a Form Label (form.label), a Form Control (form.control), and a Form Message (form.message). A Form Label is a label element. A Form Control is where the user enters the input or makes selections. By default, the Form Control is a input. Using other form components to construct the Form Control is supported. To do that, set the prop as_child=True on the Form Control.

The Form Message is a validation message which is automatically wired (functionality and accessibility). When the Form Control determines the input is invalid, the Form Message is shown. The match prop is to enable client side validation. To perform server side validation, both the force_match prop of the Form Control and the server_invalid prop of the Form Field are set together.

The Form Submit is by default a button that submits the form. To use another button component as a Form Submit, include that button as a child inside form.submit and set the prop as_child=True.

The on_submit prop of the Form Root accepts an event handler. It is called with the submitted form data dictionary. To clear the form after submission, set the reset_on_submit=True prop.

As previously mentioned, the various pieces of data in the form are submitted together as a dictionary. The form control or the input components must have the name attribute. This name is the key to get the value from the form data dictionary. If no validation is needed, the form type components such as Checkbox, Radio Groups, TextArea can be included directly under the Form Root instead of inside a Form Control.

box1 checkbox

1
2
3
box5 switch
box6 slider

Results

Server side validation is done through Computed Vars on the State. The Var should return a boolean flag indicating when input is invalid. Set that Var on both the server_invalid prop of form.field and the force_match prop of form.message. There is an example how to do that in the Final Example.

The final example shows a form that collects username and email during sign-up and validates them using server side validation. When server side validation fails, messages are displayed in red to show what is not accepted in the form, and the submit button is disabled. After submission, the collected form data is displayed in texts below the form and the form is cleared.

Username cannot be empty
A valid Email is required

Username submitted:

Email submitted:

API Reference

rx.form

The Form component.

PropType | ValuesDefaultInteractive
access_key
str
-
auto_capitalize
"off" | "none" | ...
-
content_editable
Union["inherit" | "plaintext-only", bool]
-
context_menu
str
-
dir
str
-
draggable
bool
-
enter_key_hint
"enter" | "done" | ...
-
hidden
bool
-
input_mode
"none" | "text" | ...
-
item_prop
str
-
lang
str
-
role
"alert" | "alertdialog" | ...
-
slot
str
-
spell_check
bool
-
tab_index
int
-
title
str
-
as_child
bool
-
accept
str
-
accept_charset
str
-
action
str
-
auto_complete
str
-
enc_type
str
-
method
str
-
name
str
-
no_validate
bool
-
target
str
-
reset_on_submit
bool
-
handle_submit_unique_name
str
-

Event Triggers

See the full list of default event triggers
TriggerDescription
on_submitFired when the form is submitted.
on_clear_server_errorsFired when the errors are cleared.

rx.form.root

The root component of a radix form.

PropType | ValuesDefaultInteractive
access_key
str
-
auto_capitalize
"off" | "none" | ...
-
content_editable
Union["inherit" | "plaintext-only", bool]
-
context_menu
str
-
dir
str
-
draggable
bool
-
enter_key_hint
"enter" | "done" | ...
-
hidden
bool
-
input_mode
"none" | "text" | ...
-
item_prop
str
-
lang
str
-
role
"alert" | "alertdialog" | ...
-
slot
str
-
spell_check
bool
-
tab_index
int
-
title
str
-
as_child
bool
-
accept
str
-
accept_charset
str
-
action
str
-
auto_complete
str
-
enc_type
str
-
method
str
-
name
str
-
no_validate
bool
-
target
str
-
reset_on_submit
bool
-
handle_submit_unique_name
str
-

Event Triggers

See the full list of default event triggers
TriggerDescription
on_submitFired when the form is submitted.
on_clear_server_errorsFired when the errors are cleared.

rx.form.field

A form field component.

PropType | ValuesDefaultInteractive
as_child
bool
-
name
str
-
server_invalid
bool
-

rx.form.control

A form control component.

PropType | ValuesDefaultInteractive
as_child
bool
-

rx.form.label

A form label component.

PropType | ValuesDefaultInteractive
as_child
bool
-

rx.form.message

A form message component.

PropType | ValuesDefaultInteractive
as_child
bool
-
name
str
-
match
"badInput" | "patternMismatch" | ...
-
force_match
bool
-

rx.form.submit

A form submit component.

PropType | ValuesDefaultInteractive
as_child
bool
-

Built with Reflex