Bird
0
0

Given this Angular reactive form control initialization:

medium📝 state output Q13 of 15
Angular - Reactive Forms
Given this Angular reactive form control initialization:
this.form = new FormGroup({
username: new FormControl('', Validators.required)
});

// User focuses on username input, types 'John', then leaves the input.

What will be the values of dirty, touched, and valid for username after these actions?
A<code>dirty: true, touched: true, valid: false</code>
B<code>dirty: false, touched: true, valid: true</code>
C<code>dirty: true, touched: false, valid: false</code>
D<code>dirty: true, touched: true, valid: true</code>
Step-by-Step Solution
Solution:
  1. Step 1: Analyze user actions on the form control

    User focused and left the input, so touched is true. User changed the value from empty string to 'John', so dirty is true.
  2. Step 2: Check validation status

    The control has Validators.required. Since 'John' is not empty, the control is valid, so valid is true.
  3. Final Answer:

    dirty: true, touched: true, valid: true -> Option D
  4. Quick Check:

    Changed + left + valid input = dirty, touched, valid true [OK]
Quick Trick: Changed input + left focus + valid value = all true [OK]
Common Mistakes:
MISTAKES
  • Assuming dirty is false if user leaves input
  • Confusing touched with dirty
  • Thinking valid is false if user typed anything

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes