A custom validator is a function you write to check if a form field's input meets specific rules you define, like checking if a password is strong enough.
Click to reveal answer
beginner
How do you attach a custom validator to a TextFormField in Flutter?
You pass your validator function to the TextFormField's 'validator' property. This function returns null if input is valid or an error message string if invalid.
Click to reveal answer
beginner
What should a custom validator function return when the input is valid?
It should return null to indicate the input passes validation.
Click to reveal answer
intermediate
Why use custom validators instead of built-in validators?
Custom validators let you check rules specific to your app, like matching passwords or checking a username format, which built-in validators might not cover.
Click to reveal answer
beginner
What is a simple example of a custom validator that checks if a field is not empty?
A function that returns 'Please enter some text' if the input is empty, otherwise returns null.
Click to reveal answer
What does a Flutter validator function return when the input is invalid?
AAn error message string
Bnull
Ctrue
Dfalse
✗ Incorrect
A validator returns an error message string to show the user what is wrong with their input.
Where do you assign a custom validator in a Flutter form?
AIn the TextFormField's controller
BIn the Scaffold widget
CIn the MaterialApp widget
DIn the TextFormField's validator property
✗ Incorrect
The validator property of TextFormField is where you assign your custom validation function.
Which of these is a valid return value from a Flutter validator for valid input?
Anull
B"Input is valid"
C"" (empty string)
Dfalse
✗ Incorrect
Returning null means the input passed validation with no errors.
Why might you create a custom validator instead of using built-in ones?
ATo disable validation
BTo add app-specific rules like password strength
CTo avoid using TextFormField
DBecause built-in validators are slower
✗ Incorrect
Custom validators let you enforce rules unique to your app's needs.
What happens if a validator returns a non-null string?
AThe app crashes
BThe form submits successfully
CThe form field shows an error message
DThe input is cleared
✗ Incorrect
Returning a string shows that string as an error message below the form field.
Explain how to create and use a custom validator in a Flutter form.
Think about what the validator function needs to do and where it goes.
You got /3 concepts.
Describe why custom validators are important in mobile app forms.
Consider what built-in validators might miss.
You got /4 concepts.
Practice
(1/5)
1. What does a custom validator function in Flutter return when the input is valid?
easy
A. false
B. null
C. true
D. An empty string
Solution
Step 1: Understand validator return type
A custom validator returns a String error message if input is invalid, otherwise null.
Step 2: Identify valid input return
When input is valid, the validator returns null to indicate no error.
2. Which of the following is the correct syntax for a simple custom validator in Flutter that checks if input is empty?
easy
A. validator: (value) => value.isEmpty ? null : 'Enter text'
B. validator: (value) { if (value.isEmpty) return 'Enter text'; }
C. validator: (value) => if (value.isEmpty) 'Enter text' else null
D. validator: (value) => value!.isEmpty ? 'Enter text' : null
Solution
Step 1: Check correct Dart syntax for validator
The validator must return a String or null. validator: (value) => value!.isEmpty ? 'Enter text' : null uses a concise lambda with a ternary operator correctly.
Step 2: Identify null safety and return values
validator: (value) => value!.isEmpty ? 'Enter text' : null uses value! to assert non-null and returns error string if empty, else null.
D. The validator returns null but error shows anyway
Solution
Step 1: Analyze input length check
The validator checks if value is null or length less than 5, then returns 'Too short'.
Step 2: Apply input "abc" to condition
"abc" length is 3, which is less than 5, so validator returns 'Too short'.
Final Answer:
The form shows error message 'Too short' -> Option C
Quick Check:
Input length < 5 triggers error message [OK]
Hint: Check input length against condition to predict error [OK]
Common Mistakes:
Ignoring null check and assuming no error
Confusing return null with error message
Assuming validator crashes on short input
4. Identify the error in this custom validator code:
validator: (value) {
if (value.isEmpty) {
return 'Cannot be empty';
}
}
medium
A. Using isEmpty on nullable value without null check
B. Missing return null for valid input
C. Returning a string instead of boolean
D. Validator should not use if statements
Solution
Step 1: Check null safety on value
value is nullable String?, so calling value.isEmpty without null check causes error.
Step 2: Identify missing null check
Must check if value is null before calling isEmpty to avoid runtime error.
Final Answer:
Using isEmpty on nullable value without null check -> Option A
Quick Check:
Always check for null before isEmpty on nullable String [OK]
Hint: Check for null before calling isEmpty on value [OK]
Common Mistakes:
Assuming value is non-null without checking
Not returning null for valid input (though important)
Confusing return types of validator
5. You want to create a custom validator that checks if input is a valid email and also ends with ".com". Which validator implementation correctly combines these checks?
hard
A. validator: (value) {
if (value == null || !value.contains('@')) return 'Invalid email';
if (!value.endsWith('.com')) return 'Must end with .com';
return null;
}
B. validator: (value) => value!.contains('@') && value.endsWith('.com') ? null : 'Invalid email or domain';
C. validator: (value) {
if (value == null) return null;
if (!value.contains('@') || !value.endsWith('.com')) return 'Invalid email';
return null;
}
D. validator: (value) {
if (value == null || value.contains('@') && value.endsWith('.com')) return null;
return 'Invalid email';
}
Solution
Step 1: Check null and '@' presence
validator: (value) {
if (value == null || !value.contains('@')) return 'Invalid email';
if (!value.endsWith('.com')) return 'Must end with .com';
return null;
} first checks if value is null or missing '@', returning 'Invalid email'.
Step 2: Check if ends with '.com'
Then it checks if value ends with '.com', returning specific error if not.
Step 3: Return null if all checks pass
If both checks pass, it returns null indicating valid input.
Final Answer:
validator: (value) {
if (value == null || !value.contains('@')) return 'Invalid email';
if (!value.endsWith('.com')) return 'Must end with .com';
return null;
} -> Option A
Quick Check:
Separate checks with clear error messages [OK]
Hint: Check conditions stepwise, return null only if all pass [OK]
Common Mistakes:
Combining conditions incorrectly with && or ||
Returning null on null input without error
Not checking null before calling methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.