Bird
0
0

You want to create a FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?

hard🚀 Application Q15 of 15
Angular - Reactive Forms
You want to create a FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?
Anew FormControl('', { required: true })
Bnew FormControl('', required)
Cnew FormControl(Validators.required, '')
Dnew FormControl('', [Validators.required])
Step-by-Step Solution
Solution:
  1. Step 1: Recall FormControl constructor signature

    The constructor accepts initial value as first argument, and validators as second argument in an array.
  2. Step 2: Analyze each option

    new FormControl('', { required: true }) uses invalid options object. new FormControl('', required) references undefined required. new FormControl(Validators.required, '') swaps arguments incorrectly. new FormControl('', [Validators.required]) correctly passes an array with Validators.required.
  3. Final Answer:

    new FormControl('', [Validators.required]) -> Option D
  4. Quick Check:

    Validators go in array as second argument [OK]
Quick Trick: Pass validators as array in second argument [OK]
Common Mistakes:
MISTAKES
  • Confusing with template-driven {required: true}
  • Forgetting Validators prefix
  • Swapping argument order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes