Flutter - How To Validate A Form (TextFormField Validation)

preview_player
Показать описание
Learn exactly how the validator property from TextFormField works. I'm using a Name and an Age input as examples. We'll require the age to be 18 or over and the name to have at least 3 characters in order to be validated.

This will be an example that is simple enough for beginners to understand the concept and also with some specificities that could be helpful.

Of course the validator can become as complex as you wish it to be.

But the way form validation works in Flutter is you have include all the TextFormFields inside the Form widget. Now this can be done at any level of the widget tree. Just like in the example, where I make Form be the parents of a Padding widget that is the parent of a Column widget that has the TextFormFields as its children.

After that, you must use the "validator" property of the TextFormField (not present in the TextField widget). This property takes an anonymous function that carries a value as the argument and that expects a String to be returned. Within the function, you have to define the logic behind your validation by using the value given by the function.

Once that is out of the way, you have to define a way to submit the form and you access the Form Key's current state to check if it's valid (this returns a true or false bool).

If it's valid, everything is great, if it's not, an error message will appear.

As simple as that, even if you're a beginner!

00:00 - Intro
00:30 - Creating A Form Key
00:47 - Using the Form Widget
01:08 - Validator Property
01:30 - Setting Validating Rules
02:45 - Validating Rules for Age
04:04 - Validation Submission
04:46 - Trying Out The Validator
05:19 - Flutter Mentor Out

#flutter #form #validation

Credits:

OUTRO SONG:

Keywords:
flutter form validation example, flutter form validation tutorial, flutter form validation
Рекомендации по теме
Комментарии
Автор

Man! Make more videos. As many as you can. every single video is something I just find out. Even though its been almost an year doing flutter. Your way of teaching those skills is immaculate

MrFACTS-mkib
Автор

i've never seen a way to explain this shit so easy, thanks man good video!

karlonall
Автор

Hey thanks for the video.
But I wonder can we use only one validator property and do multi validation? like email, phone, and name at one widget.

ahmetfarukakgun
Автор

That beat at the end is toooo fire bro 😭

agoodmansaid
Автор

A question, I get an error when I do
It asks me to use !. before validate(), then how do I validate my form?

muhammadumarwaseem
Автор

How can we validate audio file or any document file?

saratm
Автор

If you build an app
personal_expenses
How do I prevent negative numeric values from being entered into a text field in a form?
for example
0.01
If we assume that the input field for salary
I don't want the user to enter negative values
To make addition and subtraction easier
For salary, savings and expenses
I need a function with a condition and an error message that appears in Dialog

actionscript
Автор

Great video! Could you make a video on making a quiz app in flutter?

tomerdun
Автор

Great tutorial.
Please can you do a validation form where the user will enter phone, password and confirm password and their email will be verified before being stored in firebase

japcrosdel
Автор

Hello. I have a problem with this validator. Even if its invalid, it shows the error and proceed to perform the action 🥺🥺 I have tried to so many ways to fix this, but it isn't working. Please can u help me?. It shows error and proceed to perform the action

doulainc.
Автор

A TextFormField does not really need a Form parent to work. Not sure why I don't find a single video showing this.

iAnguel