Skip to content

Commit 5f65bb2

Browse files
committed
added form validation
1 parent 9b85b48 commit 5f65bb2

8 files changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
slug: form-validation
3+
title: Form Validation
4+
tags: []
5+
description: Learn how to add Form Validation widget in your FlutterFlow app.
6+
sidebar_position: 2
7+
---
8+
9+
10+
# Form Validation
11+
12+
The Form widget allows you to validate the user input and display a message if validation fails. This widget can contain any number of form fields, which are typically input fields such as [TextFields](form-widgets/textfield), [Dropdown](form-widgets/dropdown), [RadioButton](form-widgets/radiobutton), or [Date/Time picker](#).
13+
14+
For example, you could use it to check if a user has given a valid email and password. This makes it easy to handle user input and ensure that the data is correct before it is submitted to the server or stored locally.
15+
16+
## Adding Form widget
17+
18+
Let's see how to add a *Form* widget by building a signup example. Here's how it looks:
19+
20+
![Validating input fields in a signup form](../imgs/form-demo.gif)
21+
22+
Building and validating a *Form* includes the following steps:
23+
24+
1. [Adding input fields](#1-adding-input-fields)
25+
2. [Adding validations](#2-adding-validations)
26+
3. [Adding validate action](#3-adding-validate-action)
27+
28+
### 1. Adding input fields
29+
30+
A form widget can only validate if there are any input fields. Here's an example of adding input fields for the signup form.
31+
32+
1. First, add the **Form** widget itself from the **Form Elements**.
33+
2. Inside the form, add the [**Column**](#) widget from the **Layout Elements** tab.
34+
3. Now, add two [**TextFields**](form-widgets/textfield) (one for email and one for password).
35+
4. Add a [**Button**](#) widget and then add [Date/Time Picker](#) action to get the date of birth.
36+
5. Add one more [**Button**](#) to validate and submit the form.
37+
38+
Here's how it looks:
39+
40+
![Input fields](../imgs/fv-input-fields.avif)
41+
42+
### 2. Adding validations
43+
44+
Validation refers to the process of checking user input for correctness and ensuring that it meets certain criteria or requirements. This can include checking for the presence of required fields, verifying that a value is within a certain range or format, or validating against the custom pattern.
45+
46+
After adding input fields, they will be available to be validated using the form widget properties. Here's how you do it:
47+
48+
1. Select the **Form** widget, and move to the **Properties Panel > Validate** section.
49+
2. Identify the **TextField** on which you would like to add the validation and tick the box on the right side.
50+
1. Inside the **Error Message** input box, provide the message that will be displayed (below the *TextField*) if a user leaves the *TextField* empty.
51+
2. You can also specify the **Min Required Character** and **Max Allowed Characters**.
52+
1. **Min Required Character**: This is the minimum character required for the validation to pass. For example, If you provide a value as 9 and a user enters the value as *[email protected]* (which is 6 characters), **then the validation fails, and an error message will be displayed.
53+
1. Inside the **Minimum Character** **Error Text** input box, provide the message that will be displayed if a user doesn't provide the min required characters.
54+
2. **Max Allowed Characters**: This is the maximum number of characters allowed for the validation to pass. For example, If you provide a value of 15 and a user enters a password that exceeds 15 characters, then the validation fails, and an error message will be displayed.
55+
1. Inside the **Max Allowed Characters** **Error Text** input box, provide the message that will be displayed if a user enters more than the maximum allowed characters.
56+
57+
<div style={{
58+
position: 'relative',
59+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
60+
height: 0,
61+
width: '100%'}}>
62+
<iframe
63+
src="https://demo.arcade.software/HYbrOKwrI671of7LfRvf?embed&show_copy_link=true"
64+
title=""
65+
style={{
66+
position: 'absolute',
67+
top: 0,
68+
left: 0,
69+
width: '100%',
70+
height: '100%',
71+
colorScheme: 'light'
72+
}}
73+
frameborder="0"
74+
loading="lazy"
75+
webkitAllowFullScreen
76+
mozAllowFullScreen
77+
allowFullScreen
78+
allow="clipboard-write">
79+
</iframe>
80+
</div>
81+
<p></p>
82+
83+
3. You can also choose to validate the input using our predefined validators or by creating the custom one. To do so, you can set the **Text Validator** to the one you need.
84+
1. If the required validation is not on the list, you can select **Custom Regex** and specify your own **Regex (Dart/JS)**. Here are some examples of *Custom Regex*:
85+
86+
87+
| Examples | Regex (Dart/JS) |
88+
| --- | --- |
89+
| IP address (e.g., 192.168.1.1) | ^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$ |
90+
| Time in the 24-hour format (e.g., 13:45) | ^([01]?[0-9]|2[0-3]):[0-5][0-9]$ |
91+
2. Also, provide a message in **Invalid Text Error Text**. This will be displayed If validation for the *Custom Regex* fails.
92+
4. You can also add validation on certain actions that can be used inside the form, such as *Date/Time Picke*r and *PlacePicker*. To do so, find the action name and tick the box on the right side.
93+
1. Now you must enable **Add Action on Error** and set the **Action Type** to the appropriate one. This will be triggered if the validation fails. For example, in this case, if a form is submitted without selecting the birth date, you can add a Show Snackbar action asking a user to select the date.
94+
95+
96+
![Validating Date/Time picker](../imgs/validating-date-time-picker.png)
97+
98+
### 3. Adding validate [Action]
99+
100+
Follow the steps below to add this action to any widget.
101+
102+
1. Select the **Widget** (e.g., Container, Button, etc.) on which you want to add the action.
103+
2. Select **Actions** from the properties panel (the right menu), If it's the first action, click **+ Add Action** button. Otherwise, click the "**+**" button below the previous action tile (inside *Action Flow Editor*) and select **Add Action**.
104+
3. Search and select the **Validate Form** (under *Widget/UI Interactions*) action.
105+
4. Set the **Select Form to Validate** to your **Form name**.
106+
5. You can chain the next action that will be triggered if the validation passes.
107+
108+
<div style={{
109+
position: 'relative',
110+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
111+
height: 0,
112+
width: '100%'}}>
113+
<iframe
114+
src="https://demo.arcade.software/BlAIvaCybghOW8f5AHFW?embed&show_copy_link=true"
115+
title=""
116+
style={{
117+
position: 'absolute',
118+
top: 0,
119+
left: 0,
120+
width: '100%',
121+
height: '100%',
122+
colorScheme: 'light'
123+
}}
124+
frameborder="0"
125+
loading="lazy"
126+
webkitAllowFullScreen
127+
mozAllowFullScreen
128+
allowFullScreen
129+
allow="clipboard-write">
130+
</iframe>
131+
</div>
132+
<p></p>
133+
134+
## Auto validating
135+
136+
Rather than displaying an error message after the user submits the form, you can provide real-time feedback as they type in the *TextField* widget to indicate validation errors. This feature can be particularly useful for lengthy forms where it can save the user's time and effort.
137+
138+
To auto validate a form, select **TextField >** move to the **Properties Panel > Add validations >** and then enable the **Automatically Validate**.
139+
140+
![Auto validating](../imgs/auto-validating-demo.gif)
141+
142+
![Enabling auto validate](../imgs/enable-auto-validate.avif)
143+
144+
145+
## Validating a Form on TextField On Submit
146+
147+
You can also validate a form when you are done entering a value inside the *TextField* using the *On Submit* action.
148+
149+
![Validating a Form on TextField On Submit](../imgs/validating-form-on-textfield-onsubmit.gif)
150+
151+
152+
To validate a form on *TextField* *On Submit*:
153+
154+
1. Select the **TextField** widget and select **Actions** from the Properties panel.
155+
2. Click **+ Add Action** button, and ensure that the **Type of Action** is set to **On Submit**.
156+
3. Search, and select the **Validate Form** (under UI Interactions) action.
157+
4. Set the **Select Form to Validate** to your **Form name**.
158+
159+
![Validating a Form on TextField On Submit](../imgs/adding-validating-form-on-textfield-onsubmit.gif)
160+
161+
---
162+
163+
## Video guide
164+
165+
If you prefer watching a video tutorial, here's the one for you:
166+
167+
<div style={{
168+
position: 'relative',
169+
paddingBottom: 'calc(56.67989417989418% + 41px)', // Keeps the aspect ratio and additional padding
170+
height: 0,
171+
width: '100%'}}>
172+
<iframe
173+
src="https://www.youtube.com/embed/RCLMIyuJoL4"
174+
title=""
175+
style={{
176+
position: 'absolute',
177+
top: 0,
178+
left: 0,
179+
width: '100%',
180+
height: '100%',
181+
colorScheme: 'light'
182+
}}
183+
frameborder="0"
184+
loading="lazy"
185+
webkitAllowFullScreen
186+
mozAllowFullScreen
187+
allowFullScreen
188+
allow="clipboard-write">
189+
</iframe>
190+
</div>
191+
<p></p>
946 KB
Loading
1.01 MB
Loading
Binary file not shown.
2.3 MB
Loading
Binary file not shown.
29 KB
Loading
738 KB
Loading

0 commit comments

Comments
 (0)