0
0
Fluttermobile~20 mins

Form validation rules in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when submitting a Flutter form with an empty required field?
Consider a Flutter form with a TextFormField that has a validator checking if the field is empty. What will the user see if they submit the form without entering any text?
Flutter
TextFormField(
  validator: (value) {
    if (value == null || value.isEmpty) {
      return 'Please enter some text';
    }
    return null;
  },
),
AThe keyboard closes but no error message is shown.
BThe form submits successfully without any message.
CThe app crashes with a null pointer exception.
DAn error message 'Please enter some text' appears below the field, and the form does not submit.
Attempts:
2 left
💡 Hint
Think about what the validator function returns when the field is empty.
📝 Syntax
intermediate
2:00remaining
Which validator code correctly checks if input is a valid email?
You want to validate that a TextFormField input is a valid email using a simple regex. Which validator code snippet is correct?
Avalidator: (value) { if (value == null || !RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$").hasMatch(value)) return 'Invalid email'; else return null; },
Bvalidator: (value) { if (value == null) return null; if (!RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$").hasMatch(value)) return 'Invalid email'; },
Cvalidator: (value) => value != null && RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$").hasMatch(value) ? 'Invalid email' : null,
Dvalidator: (value) => RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$").hasMatch(value ?? '') ? null : 'Invalid email',
Attempts:
2 left
💡 Hint
The validator must return null if valid, error string if invalid.
lifecycle
advanced
2:00remaining
When is the validator function called in Flutter forms?
In Flutter, when does the validator function of a TextFormField get called during form usage?
AOnly when the form's validate() method is called.
BOnly when the field loses focus.
CEvery time the user types a character in the field.
DWhen the form is built and never again.
Attempts:
2 left
💡 Hint
Think about how Flutter triggers validation explicitly.
🔧 Debug
advanced
2:00remaining
Why does this Flutter form validator always show an error even with valid input?
Look at this validator code: validator: (value) { if (value!.isEmpty) { return 'Required'; } if (!value.contains('@')) { return 'Invalid email'; } return null; } Why might this cause a runtime error?
Flutter
validator: (value) {
  if (value!.isEmpty) {
    return 'Required';
  }
  if (!value.contains('@')) {
    return 'Invalid email';
  }
  return null;
}
ABecause contains('@') is not a valid method on String.
BBecause value can be null, using value! causes a crash if null is passed.
CBecause the validator must return a boolean, not a string.
DBecause the validator function is missing a return statement.
Attempts:
2 left
💡 Hint
Consider what happens if the field is empty and value is null.
🧠 Conceptual
expert
3:00remaining
What is the best way to validate multiple fields with dependencies in Flutter forms?
You have two fields: password and confirm password. You want to validate that confirm password matches password. Which approach correctly handles this in Flutter form validation?
AValidate only password field and ignore confirm password field validation.
BUse separate validators on each field without cross-checking values.
CUse a validator on confirm password that accesses the password field's controller text to compare values.
DUse a global variable to store password and compare in confirm password validator.
Attempts:
2 left
💡 Hint
Think about how to access another field's value inside a validator.