Recall & Review
beginner
What is a custom validator in Angular?
A custom validator is a function you write to check if a form control's value meets specific rules beyond built-in checks. It helps you create your own validation logic.
Click to reveal answer
beginner
How do you apply a custom validator to a form control in Angular?
You add the custom validator function to the control's validators array when creating the form control, usually inside FormBuilder or FormControl constructor.
Click to reveal answer
beginner
What should a custom validator function return if the control value is valid?
It should return null to indicate the value passed the validation.
Click to reveal answer
beginner
What does a custom validator return if the control value is invalid?
It returns an object with a key describing the error and a value with details, for example: { 'myError': true }.
Click to reveal answer
beginner
Why use custom validators instead of built-in validators?
Because sometimes your form needs special rules that Angular's built-in validators don't cover, like checking a username format or matching passwords.
Click to reveal answer
What does a custom validator function receive as input?
✗ Incorrect
A custom validator function receives the FormControl object to access its current value and state.
What should a custom validator return if the control value is valid?
✗ Incorrect
Returning null means the control passed validation with no errors.
How do you add a custom validator to a FormControl?
✗ Incorrect
You add custom validators as the second argument (or inside an array) when creating the FormControl.
What is the purpose of the error object returned by a custom validator?
✗ Incorrect
The error object tells Angular which validation rule failed and can be used to show messages.
Which Angular module provides the tools to create reactive forms with custom validators?
✗ Incorrect
ReactiveFormsModule is needed to build reactive forms and use custom validators.
Explain how to create and use a custom validator in Angular reactive forms.
Think about the function signature and how Angular expects validators.
You got /5 concepts.
Describe why and when you would create a custom validator instead of using built-in validators.
Consider cases where built-in validators are not enough.
You got /4 concepts.