Showing validation errors helps users know what they need to fix in a form. It makes forms easier and clearer to use.
Showing validation errors in Angular
import { Component } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; @Component({ standalone: true, selector: 'app-simple-form', template: ` <input [formControl]="nameControl" aria-describedby="nameError" /> <div *ngIf="nameControl.invalid && nameControl.touched" id="nameError" role="alert"> <small *ngIf="nameControl.errors?.['required']">Name is required.</small> <small *ngIf="nameControl.errors?.['minlength']">Name must be at least 3 characters.</small> </div> ` }) export class SimpleFormComponent { nameControl = new FormControl('', [Validators.required, Validators.minLength(3)]); }
Use FormControl with validators to track input and errors.
Show error messages only when the control is invalid and the user has touched it.
nameControl = new FormControl('', Validators.required); // In template: // <div *ngIf="nameControl.invalid && nameControl.touched"> // <small *ngIf="nameControl.errors?.['required']">This field is required.</small> // </div>
emailControl = new FormControl('', [Validators.required, Validators.email]); // In template: // <div *ngIf="emailControl.invalid && emailControl.touched"> // <small *ngIf="emailControl.errors?.['required']">Email is required.</small> // <small *ngIf="emailControl.errors?.['email']">Enter a valid email.</small> // </div>
passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]); // In template: // <div *ngIf="passwordControl.invalid && passwordControl.touched"> // <small *ngIf="passwordControl.errors?.['required']">Password is required.</small> // <small *ngIf="passwordControl.errors?.['minlength']">Password must be at least 6 characters.</small> // </div>
This component shows a login form with email and password fields. It shows error messages below each input if the user leaves the field empty or types invalid data. The login button is disabled until both fields are valid.
import { Component } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; @Component({ standalone: true, selector: 'app-login-form', template: ` <form> <label for="email">Email:</label> <input id="email" type="email" [formControl]="emailControl" aria-describedby="emailError" /> <div *ngIf="emailControl.invalid && emailControl.touched" id="emailError" role="alert" style="color: red;"> <small *ngIf="emailControl.errors?.['required']">Email is required.</small> <small *ngIf="emailControl.errors?.['email']">Please enter a valid email address.</small> </div> <label for="password">Password:</label> <input id="password" type="password" [formControl]="passwordControl" aria-describedby="passwordError" /> <div *ngIf="passwordControl.invalid && passwordControl.touched" id="passwordError" role="alert" style="color: red;"> <small *ngIf="passwordControl.errors?.['required']">Password is required.</small> <small *ngIf="passwordControl.errors?.['minlength']">Password must be at least 6 characters.</small> </div> <button type="submit" [disabled]="emailControl.invalid || passwordControl.invalid">Login</button> </form> ` }) export class LoginFormComponent { emailControl = new FormControl('', [Validators.required, Validators.email]); passwordControl = new FormControl('', [Validators.required, Validators.minLength(6)]); }
Use aria-describedby and role="alert" for better accessibility so screen readers announce errors.
Only show errors after the user has touched the input to avoid confusion.
Disable submit buttons when the form is invalid to prevent bad submissions.
Validation errors help users fix mistakes in forms.
Show errors only when the user interacts with the field and it is invalid.
Use Angular's FormControl and Validators to manage validation easily.
Practice
Solution
Step 1: Understand user experience best practice
Showing errors only after user interaction prevents confusion and clutter.Step 2: Angular validation behavior
Angular'stouchedordirtyflags help detect user interaction before showing errors.Final Answer:
Only after the user has touched or changed the field and it is invalid -> Option AQuick Check:
Show errors after user interaction = D [OK]
- Showing errors immediately on page load
- Showing errors without checking if field is invalid
- Waiting only for form submit to show errors
Solution
Step 1: Identify Angular directives for conditionally showing elements
*ngIfis used to conditionally display content based on an expression.Step 2: Check correct condition for validation errors
UsemyControl.invalid && myControl.touchedto show errors only when invalid and user touched the field.Final Answer:
*ngIf="myControl.invalid && myControl.touched" -> Option AQuick Check:
*ngIf with invalid and touched = A [OK]
- Using *ngFor instead of *ngIf for condition
- Checking valid instead of invalid
- Using *ngModel incorrectly for validation display
<input [formControl]="emailControl"> <div *ngIf="emailControl.invalid && emailControl.touched"> <small *ngIf="emailControl.errors?.required">Email is required.</small> <small *ngIf="emailControl.errors?.email">Invalid email format.</small> </div>
What will be shown if the user focuses and leaves the input empty?
Solution
Step 1: Analyze validation conditions
The input is empty and touched, sorequirederror is true, butemailerror is false because empty triggers required first.Step 2: Determine which message shows
Only therequirederror message is shown becauseemailControl.errors?.requiredis true.Final Answer:
The message 'Email is required.' will be displayed -> Option CQuick Check:
Empty + touched triggers required error = B [OK]
- Assuming invalid email message shows on empty input
- Showing no message on touched empty field
- Showing both messages simultaneously
<div *ngIf="emailControl.errors.required">Email is required.</div>
But the error never shows even when the field is empty and touched. What is the likely problem?
Solution
Step 1: Understand when errors should show
Errors should show only after user interaction, so checkingtouchedis important.Step 2: Identify missing condition
The code only checkserrors.requiredbut not if the user touched the field, so error stays hidden initially.Final Answer:
You forgot to check if the control is touched before showing errors -> Option BQuick Check:
Check touched before errors to show messages = C [OK]
- Showing errors without user interaction check
- Confusing invalid with errors property
- Adding unrelated validators like email for required error
Solution
Step 1: Ensure errors show only after user interaction
Check bothinvalidandtouchedto avoid showing errors prematurely.Step 2: Show specific minlength error message
UsepasswordControl.errors?.minlengthinside the conditional block to display the custom message.Final Answer:
Correctly combines invalid, touched, and minlength error check -> Option DQuick Check:
Check invalid and touched with minlength error = A [OK]
- Showing errors without checking touched
- Showing errors on invalid without user interaction
- Missing specific error condition for minlength
Ready to prove it?
Pass the test and this pattern moves unknown
