Angular Web Framework Reactive Forms & Validation 2 — Questions and Answers
Question 1: Which method on a FormGroup marks all descendant controls as touched?
- markAllAsTouched() (Correct answer)
- markAsTouched()
- updateValueAndValidity()
- markAsDirty()
Correct answer: markAllAsTouched()
`markAllAsTouched()` recursively marks every control in the group and its children as touched.
Question 2: What does the `updateOn` option set to `'blur'` do in a reactive form control?
- Triggers validation only when the field loses focus (Correct answer)
- Disables the control on blur
- Resets the control value on blur
- Marks the control dirty on blur
Correct answer: Triggers validation only when the field loses focus
Setting `updateOn: 'blur'` defers value and validity updates until the user leaves the field.
Question 3: Which validator ensures a control's value matches a given regular expression?
- Validators.pattern() (Correct answer)
- Validators.format()
- Validators.regex()
- Validators.match()
Correct answer: Validators.pattern()
`Validators.pattern()` accepts a string or RegExp and validates the control value against it.
Question 4: How do you disable a specific FormControl inside a FormGroup without removing it?
- Call control.disable()
- Set the disabled attribute in the template
- Pass { disabled: true } to FormBuilder.control()
- Both A and C are valid approaches (Correct answer)
Correct answer: Both A and C are valid approaches
You can call `control.disable()` at runtime or pass `{ disabled: true }` as the initial state object to `FormBuilder.control()`.
Question 5: What value does `formControl.value` return when the control is disabled?
- The current value is still returned (Correct answer)
- null
- undefined
- An empty string
Correct answer: The current value is still returned
A disabled control retains its value; `formControl.value` still returns the current value even when disabled.
Question 6: Which FormGroup method returns the raw value including disabled controls?
- getRawValue() (Correct answer)
- getValue()
- getAll()
- rawValue property
Correct answer: getRawValue()
`FormGroup.getRawValue()` returns the values of all controls including those that are disabled.
Question 7: What is the purpose of `AbstractControl.setErrors()` in reactive forms?
- Manually set validation errors on a control bypassing validators (Correct answer)
- Clear all errors from a control
- Trigger async validators immediately
- Reset the control to its initial state
Correct answer: Manually set validation errors on a control bypassing validators
`setErrors()` lets you programmatically assign an error map to a control, useful for server-side validation results.
Which method on a FormGroup marks all descendant controls as touched?