Bird
0
0

Consider this Angular reactive form snippet:

medium📝 Predict Output Q5 of 15
Angular - Reactive Forms
Consider this Angular reactive form snippet:
this.form = new FormGroup({
password: new FormControl('', Validators.required)
});
this.form.controls.password.setValue('');
console.log(this.form.controls.password.valid);

What will be the output in the console?
Afalse
Btrue
Cundefined
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Validators.required with empty string

    Validators.required fails if the control value is empty string, so valid will be false.
  2. Step 2: Check the valid property after setting empty value

    Since password is set to '', validation fails, so valid is false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Empty required field = valid false [OK]
Quick Trick: Empty required fields are invalid (valid = false) [OK]
Common Mistakes:
MISTAKES
  • Assuming empty string passes required validator
  • Confusing valid with dirty or touched
  • Expecting true for empty value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes