Sub topic 7
Reading Status, Touched, and Dirty State to Show Errors
This chapter assumes you can create a FormGroup, bind it with formGroup and formControlName, and attach built-in validators such as Validators.required. Here you will use the state that every control tracks to decide when to show an error message.
The State Every Control Tracks
Every FormControl, FormGroup, and FormArray extends AbstractControl, so they all expose the same state properties.
| Property | Values | Changes when | Column |
|---|---|---|---|
| `status` | `'VALID'`, `'INVALID'`, `'PENDING'`, `'DISABLED'` | Validators run, or the control is disabled | |
| `valid` / `invalid` | boolean | Same as `status` | |
| `pending` | boolean | An async validator is still running | |
| `touched` / `untouched` | boolean | The user blurs (leaves) the field | |
| `dirty` / `pristine` | boolean | The user changes the value through the UI | |
| `errors` | `ValidationErrors \ | null` | Validators return error objects |
A group's status is computed from its children. If any child is invalid, the group is invalid too.
Showing Errors at the Right Moment
A new form with a required field is invalid as soon as it loads. If you show errors whenever invalid is true, users see red text before they have typed anything. The usual rule is to show an error only when the control is invalid and the user has touched it. When the user submits, call markAllAsTouched() so the errors on fields they skipped show up too.
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-contact-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<label>
Email
<input type="email" formControlName="email" />
</label>
@if (email.invalid && email.touched) {
@if (email.hasError('required')) {
<p class="error">Email is required.</p>
} @else if (email.hasError('email')) {
<p class="error">Enter a valid email address.</p>
}
}
<button type="submit">Send</button>
<p>Status: {{ form.status }} | Dirty: {{ form.dirty }}</p>
</form>
`,
})
export class ContactFormComponent {
form = new FormGroup({
email: new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.email],
}),
});
get email() {
return this.form.controls.email;
}
onSubmit(): void {
if (this.form.invalid) {
this.form.markAllAsTouched();
return;
}
console.log(this.form.getRawValue());
}
}hasError('required') checks for one key in errors. Use getError('minlength') to read the error's payload, for example { requiredLength: 8, actualLength: 3 }, so you can show a specific message.
CSS Classes for Free
Angular adds classes to each bound element that match its state: ng-valid, ng-invalid, ng-pending, ng-touched, ng-untouched, ng-dirty, and ng-pristine. You can style errors without writing any template logic:
input.ng-invalid.ng-touched {
border-color: #c62828;
}Common Mistakes
- Checking
dirtyinstead oftouched. If a user tabs through a required field without typing, the field stays pristine and its error never appears. - Forgetting
markAllAsTouched()on submit. If the submit button does nothing and shows no errors, users can't tell what went wrong. - Treating
PENDINGas invalid. While an async validator runs,validandinvalidare both false. Checkpendingif you need to block submission. - Expecting
errorson a disabled control. Disabled controls skip validation, soerrorsisnull.
Exercise
A user loads the form above, clicks into the email field, clicks out without typing, and then clicks Send. Which state flags are true on the email control, and which error message appears?
Show answer
The control is touched (it was blurred), pristine (the value never changed through the UI), and invalid with a required error. Because email.invalid && email.touched is true and hasError('required') is true, the message "Email is required." appears. The markAllAsTouched() call in onSubmit changes nothing here because the field is already touched.
Exercise
Why would @if (email.invalid && email.dirty) hide an error that users need to see?
Show answer
dirty only becomes true after the user changes the value. A user who skips a required field, or focuses it and leaves without typing, never makes it dirty, so the required error stays hidden even after they submit.