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:
Mitsubachi by Smith The Mister https://smiththemister.bandcamp.com
Smith The Mister https://bit.ly/Smith-The-Mister-YT
Free Download / Stream: http://bit.ly/mitsubachi
Music promoted by Audio Library https://youtu.be/0IgndUb1YQI

Keywords:
flutter form validation example, flutter form validation tutorial, flutter form validation

#flutter #validation

How To Validate A Form (TextFormField Validation) - Flutter
1.85 GEEK