Reactive Forms & Validation Flashcards
7 cards from real Angular Web Framework practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Reactive Forms & Validation flashcards as text
What is the correct way to provide a custom async validator as a class implementing `AsyncValidator`?
Answer: Implement the `validate()` method returning Observable or Promise
Classes implementing `AsyncValidator` must define `validate()` returning an observable or promise that resolves to an error map or null.
Which FormArray property tells you how many controls it currently contains?
Answer: length
`FormArray.length` returns the number of controls currently in the array, mirroring the standard array API.
When using `nonNullable: true` in a FormControl, what happens when `reset()` is called?
Answer: The control resets to its initial value instead of null
A non-nullable FormControl stores its initial value and restores it on `reset()` rather than defaulting to null.
What does `Validators.composeAsync()` do?
Answer: Merges multiple async validators into one that runs them in parallel and combines their errors
`Validators.composeAsync()` combines an array of async validators, runs them concurrently, and merges all returned errors.
Which technique prevents an async validator from firing on every keystroke by caching the last checked value?
Answer: Return the cached result immediately if the value hasn't changed since the last check
Storing the previously validated value and returning the cached error map when the value is unchanged avoids redundant HTTP calls.
How do you reactively display an error message only after the user has interacted with a field?
Answer: Check control.invalid && control.touched in the template
Combining `invalid` with `touched` ensures the error appears only after the user has visited the field, not on initial load.
What is the effect of calling `control.clearValidators()` followed by `control.updateValueAndValidity()`?
Answer: Removes all sync validators and immediately re-evaluates the control, making it valid
`clearValidators()` detaches all sync validators, and `updateValueAndValidity()` forces re-evaluation so the control becomes valid (assuming no async errors).