Challenge - 5 Problems
Custom Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Custom Validator Behavior on Empty Input
Consider a Flutter TextFormField with a custom validator that checks if the input is a valid email. What will the validator return if the input is empty?
Flutter
String? emailValidator(String? value) { if (value == null || value.isEmpty) { return 'Email is required'; } final emailRegex = RegExp(r"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); if (!emailRegex.hasMatch(value)) { return 'Enter a valid email'; } return null; }
Attempts:
2 left
💡 Hint
Think about what the validator returns when the input is empty or null.
✗ Incorrect
The validator first checks if the input is null or empty and returns 'Email is required' in that case. It only checks the regex if the input is not empty.
📝 Syntax
intermediate2:00remaining
Correct Syntax for Custom Validator Function
Which of the following is the correct syntax for a Flutter custom validator function that checks if a password is at least 8 characters?
Attempts:
2 left
💡 Hint
Remember the validator must return a String? and accept nullable input.
✗ Incorrect
Validators in Flutter take a nullable String and return a nullable String error message. Option D correctly handles null and returns the error message or null.
❓ lifecycle
advanced2:00remaining
When is a Custom Validator Called in Flutter Forms?
In a Flutter form, when does the custom validator function get called for a TextFormField?
Attempts:
2 left
💡 Hint
Think about when Flutter triggers validation checks.
✗ Incorrect
Flutter calls the validator function when the form's validate() method is called, typically on form submission. It does not validate on every keystroke by default.
🔧 Debug
advanced2:00remaining
Debugging a Validator That Always Returns Null
A developer wrote this validator but it always returns null, even for invalid input:
String? validator(String? value) {
if (value!.isEmpty) {
return 'Cannot be empty';
}
if (value.length < 5) {
'Too short';
}
return null;
}
What is the cause?
Flutter
String? validator(String? value) { if (value!.isEmpty) { return 'Cannot be empty'; } if (value.length < 5) { 'Too short'; } return null; }
Attempts:
2 left
💡 Hint
Look carefully at the second if block's code.
✗ Incorrect
The second if block has a string 'Too short' but does not return it, so the function continues and returns null, meaning no error.
🧠 Conceptual
expert2:00remaining
Combining Multiple Custom Validators in Flutter
You want to combine two custom validators for a TextFormField: one checks if input is not empty, the other checks if input is numeric. How should you combine them to show the first error found?
Attempts:
2 left
💡 Hint
Think about how Flutter expects a single error message or null.
✗ Incorrect
Flutter validators return a single error message or null. To combine validators, call each and return the first error found to show one message at a time.