Bird
0
0

Why should a custom validator function in Angular always return null when the input is valid instead of undefined or other values?

hard🧠 Conceptual Q10 of 15
Angular - Reactive Forms
Why should a custom validator function in Angular always return null when the input is valid instead of undefined or other values?
ABecause Angular ignores null and treats undefined as valid
BBecause undefined causes the form to reset
CBecause Angular treats null as no error and any other value as an error
DBecause returning undefined disables the control
Step-by-Step Solution
Solution:
  1. Step 1: Understand Angular validation contract

    Angular expects validators to return null if valid, or an error object if invalid.
  2. Step 2: Effect of return values

    Returning null signals no error; any other value (including undefined) is treated as an error.
  3. Final Answer:

    Because Angular treats null as no error and any other value as an error -> Option C
  4. Quick Check:

    Return null for valid input to avoid errors [OK]
Quick Trick: Return null for valid input, anything else signals error [OK]
Common Mistakes:
MISTAKES
  • Returning undefined and expecting valid
  • Returning empty object instead of null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes