DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Display Validation Errors in Angular Forms

Show Angular validation messages at the right time by checking whether a control or form group is invalid, touched, or dirty—and by matching messages to specific error keys.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To display validation errors in Angular, add validators to a form control and show a message when that control is invalid and the user has interacted with it. Check the specific error key—such as required or minlength—so the message explains what needs fixing. Template-driven forms read state from NgModel; reactive forms read it from the corresponding control.

Choose when an error should appear

A required field can be invalid as soon as an empty form loads, but showing an error before someone has tried to use the field can be distracting. A common pattern is to display a message when the control is invalid and either touched or dirty:

As an Amazon Associate I earn from qualifying purchases.

control.invalid && (control.dirty || control.touched)

touched becomes true after the user focuses and leaves a control; dirty indicates that its value has changed. For a simpler pattern, show the message only after the field is no longer pristine. Angular’s template-driven forms guide demonstrates hiding a required-field message while the control is valid or pristine.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use text that identifies the problem and, where useful, tells the user how to correct it. Color alone does not explain why a value was rejected.

Display errors in a template-driven form

Template-driven forms commonly apply validators with familiar HTML attributes. Angular matches attributes such as required to validator functions. Export the control as an NgModel template reference variable, then check its state and error keys:

<form #profileForm="ngForm">
  <label for="name">Name</label>
  <input
    id="name"
    name="name"
    required
    minlength="3"
    [(ngModel)]="profile.name"
    #name="ngModel"
  >

  <div *ngIf="name.invalid && (name.dirty || name.touched)">
    <p *ngIf="name.hasError('required')">Enter your name.</p>
    <p *ngIf="name.hasError('minlength')">Use at least 3 characters.</p>
  </div>
</form>

The template reference provides access to the control’s validity and interaction state. Testing each error key ensures that the message matches the failed rule instead of displaying a generic warning. Angular’s validation guide also demonstrates this pattern with a custom forbidden-name error.

Display errors in a reactive form

Reactive forms define controls and validators in the component model. The template can then read each control’s errors and state. This example shows a required message and a minimum-length message:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { FormControl, FormGroup, Validators } from '@angular/forms';

profileForm = new FormGroup({
  name: new FormControl('', [Validators.required, Validators.minLength(3)]),
});
<form [formGroup]="profileForm">
  <label for="name">Name</label>
  <input id="name" formControlName="name">

  <ng-container *ngIf="profileForm.controls.name as name">
    <div *ngIf="name.invalid && (name.dirty || name.touched)">
      <p *ngIf="name.hasError('required')">Enter your name.</p>
      <p *ngIf="name.hasError('minlength')">Use at least 3 characters.</p>
    </div>
  </ng-container>
</form>

Angular’s reactive forms guide documents model-defined controls and built-in validators; its validation guide explains how control status and errors can drive displayed messages.

Handle cross-field and asynchronous validation

Cross-field rules

When a rule compares multiple fields, attach its error to the parent form group rather than assigning blame to just one child. Then check the group’s error and interaction state in the template. Angular’s example uses actorForm.hasError('unambiguousRole') and gates the message on the group being touched or dirty. This keeps the validation result associated with the combination of values that caused it.

Asynchronous rules

Async validators return a Promise or Observable. Account for the control’s pending state while the check is running so the interface does not treat an unfinished check as a final result. Angular runs asynchronous validators only after synchronous validators pass. See the official validation guide for the validation lifecycle and examples.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Template-driven or reactive forms?

Both approaches support conditional error messages. The main difference is where the form and its validation rules are defined, which affects how naturally each approach fits the form’s complexity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Approach Where rules live Custom validation Typical fit
Template-driven In the template, using attributes and directives A directive wraps the validator and registers it through NG_VALIDATORS Small or simple forms
Reactive In the component model, on controls Validator functions are attached to controls More complex forms where explicit form modeling is useful

Angular describes these trade-offs in its forms overview and validation guide. Choose the approach that matches the form; either way, display a message by checking the relevant control or group state.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.