What does a validator return when input is valid?
Validators return null for valid input, or an error message string for invalid input.
Build validated forms
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
The Form widget groups multiple input fields together and provides built-in validation capabilities. It uses a GlobalKey of type FormState to let you trigger validation and save on all fields at once, so you do not have to check each field individually.
1class LoginForm extends StatefulWidget {2@override3_LoginFormState createState() => _LoginFormState();4}56class _LoginFormState extends State<LoginForm> {7final _formKey = GlobalKey<FormState>();8String _email = '';9String _password = '';1011@override12Widget build(BuildContext context) {13return Form(14key: _formKey,15child: Column(16children: [17TextFormField(18decoration: InputDecoration(labelText: 'Email'),19validator: (value) {20if (value == null || value.isEmpty) {21return 'Please enter email';22}23if (!value.contains('@')) {24return 'Please enter a valid email';25}26return null; // Valid27},28onSaved: (value) => _email = value!,29),3031TextFormField(32decoration: InputDecoration(labelText: 'Password'),33obscureText: true,34validator: (value) {35if (value == null || value.length < 8) {36return 'Password must be at least 8 characters';37}38return null;39},40onSaved: (value) => _password = value!,41),4243ElevatedButton(44onPressed: _submitForm,45child: Text('Login'),46),47],48),49);50}5152void _submitForm() {53if (_formKey.currentState!.validate()) {54_formKey.currentState!.save();55print('Email: $_email, Password: $_password');56}57}58}
Validators are functions that receive the current field value and return either an error message string or null if the value is acceptable. You can create reusable validator functions and apply them across multiple forms in your app.
1// Email validator2String? validateEmail(String? value) {3if (value == null || value.isEmpty) {4return 'Email is required';5}6final emailRegex = RegExp(r'^[\w-\.]+@[\w-]+\.[a-z]{2,}$');7if (!emailRegex.hasMatch(value)) {8return 'Enter a valid email';9}10return null;11}1213// Password validator14String? validatePassword(String? value) {15if (value == null || value.isEmpty) {16return 'Password is required';17}18if (value.length < 8) {19return 'Password must be at least 8 characters';20}21if (!value.contains(RegExp(r'[A-Z]'))) {22return 'Password must contain uppercase letter';23}24return null;25}
Validation should happen in layers:
Client validation improves UX, but it is not a security boundary. Always validate again on the backend.
Build validated forms. Widgets are reusable UI blocks. You combine them like LEGO pieces to build screens.
Connect each concept to real app features you can build.
Sketch the widget tree first, then code from top to bottom.
Most Flutter productivity comes from understanding layout rules and composing widgets cleanly.
Validators return null for valid input, or an error message string for invalid input.
GlobalKey<FormState> provides access to validate() through currentState.
TextFormField works with Form widget to enable validation and save functionality.
save() triggers the onSaved callback on every TextFormField in the form, storing their values.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue