Full answer
Each FormControl, FormGroup and FormArray has flags that describe its state. They work in template-driven and reactive forms.
Validation state:
valid: all validators pass.invalid: at least one fails. Theerrorsproperty holds the details, for example{ required: true }.pending: an async validator is still running. While pending, a control is neither valid nor invalid.disabled: the control is excluded from validation and fromform.value.
Interaction state:
touched: the user focused the field and then left it (ablurevent happened).untouchedis the opposite.dirty: the user changed the value in the UI.pristineis the opposite.
Important detail: dirty is set by user input only. Calling control.setValue('x') in code does not make a control dirty (use markAsDirty() if you want that). touched can be set with markAsTouched(), and markAllAsTouched() on a group is the usual way to reveal all errors when the user presses submit on an incomplete form.
Using them for messages: an empty required field is invalid from the first moment, but showing "Name is required" before the user has typed anything feels rude. The common rule is @if (control.invalid && (control.touched || control.dirty)). For a submit button use form.invalid to disable it, or better, allow the click and show all errors.
Angular also adds CSS classes to the element: ng-valid, ng-invalid, ng-touched, ng-untouched, ng-dirty, ng-pristine, ng-pending. You can style input.ng-invalid.ng-touched { border-color: red; }. Group flags combine their children: a group is invalid if any child is invalid, dirty if any child is dirty.
Resetting: form.reset() sets the value to its initial state and marks controls pristine and untouched. For accessibility, connect the message to the field with aria-describedby and use role="alert" for errors that appear after submit.
import { FormControl, Validators } from '@angular/forms';
export function flagsDemo(): string[] {
const name = new FormControl('', { nonNullable: true, validators: [Validators.required] });
const snap = (step: string) =>
`${step}: valid=${name.valid} touched=${name.touched} dirty=${name.dirty}`;
const out: string[] = [snap('start')];
name.markAsTouched(); // what a blur event does
out.push(snap('after blur'));
name.setValue('Asha'); // code change
out.push(snap('after setValue'));
name.markAsDirty(); // what typing in the field does
out.push(snap('after typing'));
name.reset();
out.push(snap('after reset'));
return out;
}Angular 20.3. A Karma test records the flags of a control before typing, after a blur, after user input, after setValue, and after reset.
Key points
- `valid`, `invalid`, `pending` describe validation. `touched` means blurred. `dirty` means the user changed the value.
- `setValue` in code does not make a control dirty.
- Show errors when invalid and touched or dirty. Use `markAllAsTouched()` on submit.
- Angular adds matching `ng-*` CSS classes. Group flags combine child flags.
Common mistakes
- Showing errors on a pristine, untouched empty field.
- Expecting `setValue` to mark the control dirty.
- Forgetting that `pending` is neither valid nor invalid.
Follow-ups
- How do you show all errors when the user presses submit?
- What happens to the flags after `form.reset()`?
- Why can a group be dirty while one child is pristine?
Tips
- Contrast touched and dirty with one sentence each.
- Mention `markAllAsTouched`.
- Mention accessibility of the error message.
Evaluation rubric
- Defines each flag correctly.
- Knows how flags change and how to set them in code.
- Applies them to a good error message rule.