0
0
Angularframework~10 mins

Showing validation errors in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Showing validation errors
User inputs data
Form control updates value
Validation runs
Is input valid?
NoShow error message
Yes
No error shown
User submits form
This flow shows how user input triggers validation, which then decides if error messages appear or not.
Execution Sample
Angular
import { Component } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  standalone: true,
  selector: 'app-email-input',
  template: `
    <input [formControl]="emailControl" aria-label="Email input" />
    <div *ngIf="emailControl.invalid && emailControl.touched" role="alert">
      <span *ngIf="emailControl.errors?.['required']">Email is required.</span>
      <span *ngIf="emailControl.errors?.['email']">Enter a valid email.</span>
    </div>
  `
})
export class EmailInputComponent {
  emailControl = new FormControl('', [Validators.required, Validators.email]);
}
This Angular component shows an email input with validation errors displayed when input is invalid and touched.
Execution Table
StepUser ActionFormControl ValueValidation StatusError Messages Shown
1User focuses input and blurs, no typing yet'' (empty)Invalid (required)Email is required.
2User types 'abc' and blurs'abc'Invalid (email)Enter a valid email.
3User types 'abc@domain.com' and blurs'abc@domain.com'ValidNo errors
4User clears input and blurs'' (empty)Invalid (required)Email is required.
5User focuses input but does not type'' (empty)Invalid (required)No errors (not touched)
6User blurs input after empty'' (empty)Invalid (required)Email is required.
💡 Validation stops updating error messages when input is valid or untouched.
Variable Tracker
VariableInitialStep 1Step 2Step 3Step 4Step 5Step 6
emailControl.value'''''abc''abc@domain.com'''''''
emailControl.invalidtruetruetruefalsetruetruetrue
emailControl.errors{required: true}{required: true}{email: true}null{required: true}{required: true}{required: true}
emailControl.touchedfalsetruetruetruetruefalsetrue
Key Moments - 3 Insights
Why does the error message not show when the input is empty but not touched?
Because the template shows errors only when the control is invalid AND touched, as seen in step 5 of the execution_table.
Why does typing 'abc' show an email error instead of required error?
Because the input is not empty, so 'required' error clears, but 'email' validator fails, shown in step 2.
When does the error message disappear?
When the input becomes valid, like a proper email, errors become null and no messages show, as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What error message is shown?
AEnter a valid email.
BEmail is required.
CNo errors
DInput is valid
💡 Hint
Check the 'Error Messages Shown' column at step 2 in the execution_table.
At which step does the form control become valid?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Validation Status' column to find when it changes to 'Valid'.
If the user never touches the input, will error messages show?
AYes, always show errors
BNo, errors show only after touched
CErrors show only if input is valid
DErrors show only on submit
💡 Hint
Refer to step 5 where input is invalid but not touched and no errors show.
Concept Snapshot
Angular validation errors show only when form control is invalid and touched.
Use *ngIf to check control.invalid && control.touched.
Validators like required and email set errors.
Error messages update as user types and blurs input.
This improves user experience by not showing errors prematurely.
Full Transcript
This Angular example shows how validation errors appear for an email input. The form control starts empty and invalid because it is required. When the user focuses and types, validation runs. If the input is not a valid email, an error message appears. Errors only show if the input has been touched (user interacted and left the field). When the input becomes valid, errors disappear. This flow helps users see helpful messages only when needed, improving clarity and accessibility.