Displaying Validation Error Messages in Angular

๐Ÿ’ก Concept Name

Validation Error Messages

๐Ÿ“˜ Quick Intro

Angular forms offer built-in validation, but displaying user-friendly error messages requires custom templates or components that react to validation state changes.

๐Ÿง  Analogy / Short Story

Think of form validation like traffic lights: green means go (valid), yellow warns you to slow down (warning), and red stops you (error). Error messages guide users just like red signals caution.

๐Ÿ”ง Technical Explanation

  • โœ… **Template-driven forms:** Use `ngModel` & `
    ` blocks.
  • โœ… **Reactive forms:** Access `FormControl.errors` in template or component and display messages.
  • โœ… **Custom validators:** Return error objects like `{ invalidName: true }` and target them in message templates.
  • โœ… **Async validation:** Handle pending state and show messages after server response.

๐ŸŽฏ Purpose & Use Case

  • โœ… Improve UX by guiding users to correct input.
  • โœ… Prevent invalid data submission to backend.
  • โœ… Customize messages for localization and branding.

๐Ÿ’ป Real Code Example

<form [formGroup]="userForm" (ngSubmit)="onSubmit()">
  <label for="email">Email</label>
  <input id="email" formControlName="email" />
  <div class="error" *ngIf="email.invalid && (email.dirty || email.touched)">
    <small *ngIf="email.errors.required">Email is required.</small>
    <small *ngIf="email.errors.email">Enter a valid email.</small>
  </div>

  <button type="submit" [disabled]="userForm.invalid">Submit</button>
</form>

// Component getter
get email() { return this.userForm.get('email'); }

โ“ Interview Q&A

Q1: How do you check if a control is invalid in template-driven forms?
A: Use `#f="ngForm"` and check `f.form.controls['field'].invalid`.

Q2: Which property holds validation errors in reactive forms?
A: `FormControl.errors`.

Q3: How to display custom validator message?
A: Return error object and use `*ngIf` on that key.

Q4: What trigger states can you use?
A: `dirty`, `touched`, `submitted`.

Q5: How to disable submit on invalid form?
A: Bind `[disabled]="form.invalid"` on button.

๐Ÿ“ MCQs

Q1. Which directive shows error blocks in template-driven forms?

  • *ngFor
  • *ngIf
  • [hidden]
  • [disabled]

Q2. What does FormControl.errors return?

  • Boolean
  • Error map or null
  • String
  • Number

Q3. When does email.errors.email become true?

  • Empty value
  • Invalid email format
  • Field touched
  • Form submitted

Q4. How to check touched state?

  • control.dirty
  • control.touched
  • control.pristine
  • control.pending

Q5. Which method initializes form group?

  • FormBuilder()
  • new FormGroup
  • createForm()
  • initForm()

Q6. Which event fires submit?

  • submitClick
  • ngSubmit
  • onSubmit
  • formSubmit

Q7. To disable submit, you use?

  • (disabled)
  • [disabled]
  • disabled()
  • disable()

Q8. What class marks invalid input?

  • ng-valid
  • ng-pristine
  • ng-invalid
  • ng-touched

Q9. How to show all errors after submit?

  • dirty flag
  • touched flag
  • submitted flag
  • valid flag

Q10. Where to define custom validator?

  • Only in component
  • Only in service
  • In component or directive
  • In module

๐Ÿ’ก Bonus Insight

Abstract common error display into a reusable component to reduce template code and maintain consistent styling across forms.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More About Angular ๐Ÿš€

What is Angular vs AngularJS ๐Ÿ‘‰ Explained
Key Features of Angular ๐Ÿ‘‰ Explained
Angular Architecture ๐Ÿ‘‰ Explained
Components in Angular ๐Ÿ‘‰ Explained
Modules in Angular ๐Ÿ‘‰ Explained
Templates in Angular ๐Ÿ‘‰ Explained
Directives in Angular ๐Ÿ‘‰ Explained
Structural vs Attribute Directives ๐Ÿ‘‰ Explained
Services in Angular ๐Ÿ‘‰ Explained
Dependency Injection in Angular ๐Ÿ‘‰ Explained
Data Binding in Angular ๐Ÿ‘‰ Explained
Interpolation ๐Ÿ‘‰ Explained
Property Binding ๐Ÿ‘‰ Explained
Event Binding ๐Ÿ‘‰ Explained
Two-way Data Binding ๐Ÿ‘‰ Explained
ngModel Usage ๐Ÿ‘‰ Explained
Lifecycle Hooks ๐Ÿ‘‰ Explained
ngOnInit Hook ๐Ÿ‘‰ Explained
ngOnDestroy Hook ๐Ÿ‘‰ Explained
Pipes in Angular ๐Ÿ‘‰ Explained
Pure vs Impure Pipes ๐Ÿ‘‰ Explained
Create Custom Pipe ๐Ÿ‘‰ Explained
Angular Forms ๐Ÿ‘‰ Explained
Template-driven vs Reactive Forms ๐Ÿ‘‰ Explained
Form Controls ๐Ÿ‘‰ Explained
Form Validation ๐Ÿ‘‰ Explained
Validation Error Messages ๐Ÿ‘‰ Explained
FormGroup vs FormControl ๐Ÿ‘‰ Explained
Enable/Disable Form Fields ๐Ÿ‘‰ Explained
Reset Form ๐Ÿ‘‰ Explained
Routing in Angular ๐Ÿ‘‰ Explained
RouterModule ๐Ÿ‘‰ Explained
Configure Routes ๐Ÿ‘‰ Explained
Route Guards ๐Ÿ‘‰ Explained
CanActivate vs CanDeactivate ๐Ÿ‘‰ Explained
Lazy Loading ๐Ÿ‘‰ Explained
Preloading in Routing ๐Ÿ‘‰ Explained
Wildcard Routes ๐Ÿ‘‰ Explained
Route Parameters ๐Ÿ‘‰ Explained
Query Parameters ๐Ÿ‘‰ Explained
Programmatic Navigation ๐Ÿ‘‰ Explained
routerLink & routerLinkActive ๐Ÿ‘‰ Explained
Resolvers in Routing ๐Ÿ‘‰ Explained
Observables in Angular ๐Ÿ‘‰ Explained
RxJS in Angular ๐Ÿ‘‰ Explained
Observable vs Promise ๐Ÿ‘‰ Explained
```
Share:

Tags:


Feedback Modal Popup