Challenge - 5 Problems
Form Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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;
},
),Attempts:
2 left
💡 Hint
Think about what the validator function returns when the field is empty.
✗ Incorrect
The validator returns a string error message when the field is empty, which Flutter shows below the field and prevents form submission.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
The validator must return null if valid, error string if invalid.
✗ Incorrect
Option A returns 'Invalid email' if the value is null or does not match regex, else returns null, which is correct.
❓ lifecycle
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how Flutter triggers validation explicitly.
✗ Incorrect
Flutter calls validator functions when FormState.validate() is called, usually on form submission.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Consider what happens if the field is empty and value is null.
✗ Incorrect
Using value! assumes value is never null, but Flutter may pass null, causing a runtime error.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how to access another field's value inside a validator.
✗ Incorrect
Using the password field's controller text inside confirm password's validator allows direct comparison for matching.