Bird
Raised Fist0
Angularframework~8 mins

Showing validation errors in Angular - Performance & Optimization

Choose your learning style10 modes available
Performance: Showing validation errors
MEDIUM IMPACT
This affects interaction responsiveness and visual stability when users input data and see validation feedback.
Display form validation errors to users on input
Angular
template: `<input [(ngModel)]="name" (blur)="validateName()" /> <div [style.visibility]="showError ? 'visible' : 'hidden'">Name is required</div>`;

component: {
  name = '';
  showError = false;
  validateName() {
    this.showError = !this.name || this.name.length === 0;
  }
}
Validation runs only on blur event, reducing frequent DOM updates and reflows during typing.
📈 Performance GainSingle reflow on blur instead of multiple reflows per keystroke, improving input responsiveness.
Display form validation errors to users on input
Angular
template: `<input [(ngModel)]="name" /> <div *ngIf="!nameValid">Name is required</div>`;

component: {
  get nameValid() {
    return this.name && this.name.length > 0;
  }
}
Using getters that run on every change detection and rendering error messages with *ngIf causes frequent DOM updates and reflows.
📉 Performance CostTriggers multiple reflows on each keystroke and blocks rendering for tens of milliseconds on complex forms.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Validation on every keystroke with *ngIfHigh (many DOM toggles)Multiple reflows per inputHigh paint cost due to layout shifts[X] Bad
Validation on blur with visibility toggleLow (few DOM changes)Single reflow on blurLow paint cost, stable layout[OK] Good
Rendering Pipeline
Validation error display affects the Layout and Paint stages because showing or hiding error messages changes DOM nodes and styles.
→Layout
→Paint
→Composite
⚠️ BottleneckLayout is most expensive due to DOM changes when error messages appear or disappear.
Core Web Vital Affected
INP
This affects interaction responsiveness and visual stability when users input data and see validation feedback.
Optimization Tips
1Run validation less frequently, e.g., on blur instead of every keystroke.
2Toggle visibility of error messages instead of adding/removing DOM nodes.
3Avoid complex expressions in templates that run on every change detection.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with running validation on every keystroke?
AIt increases bundle size significantly.
BIt triggers many reflows and repaints, slowing input responsiveness.
CIt causes the page to reload.
DIt disables keyboard navigation.
DevTools: Performance
How to check: Record a performance profile while typing in the form and observe the number of reflows and layout thrashing events.
What to look for: Look for frequent Layout events and long scripting times indicating inefficient validation updates.

Practice

(1/5)
1. In Angular, when should you show validation error messages for a form field?
easy
A. Only after the user has touched or changed the field and it is invalid
B. Immediately when the form loads, regardless of user interaction
C. Only after the form is submitted, ignoring field interaction
D. Always show errors even if the field is valid

Solution

  1. Step 1: Understand user experience best practice

    Showing errors only after user interaction prevents confusion and clutter.
  2. Step 2: Angular validation behavior

    Angular's touched or dirty flags help detect user interaction before showing errors.
  3. Final Answer:

    Only after the user has touched or changed the field and it is invalid -> Option A
  4. Quick Check:

    Show errors after user interaction = D [OK]
Hint: Show errors only when user touched and invalid [OK]
Common Mistakes:
  • Showing errors immediately on page load
  • Showing errors without checking if field is invalid
  • Waiting only for form submit to show errors
2. Which Angular directive is used to check if a form control is invalid and touched in a template?
easy
A. *ngIf="myControl.invalid && myControl.touched"
B. *ngFor="let control of myControl.invalid"
C. *ngSwitchCase="myControl.valid"
D. *ngModel="myControl.errors"

Solution

  1. Step 1: Identify Angular directives for conditionally showing elements

    *ngIf is used to conditionally display content based on an expression.
  2. Step 2: Check correct condition for validation errors

    Use myControl.invalid && myControl.touched to show errors only when invalid and user touched the field.
  3. Final Answer:

    *ngIf="myControl.invalid && myControl.touched" -> Option A
  4. Quick Check:

    *ngIf with invalid and touched = A [OK]
Hint: Use *ngIf with invalid and touched to show errors [OK]
Common Mistakes:
  • Using *ngFor instead of *ngIf for condition
  • Checking valid instead of invalid
  • Using *ngModel incorrectly for validation display
3. Given this Angular template snippet:
<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?
medium
A. The message 'Invalid email format.' will be displayed
B. No message will be displayed because the field is empty
C. The message 'Email is required.' will be displayed
D. Both messages will be displayed

Solution

  1. Step 1: Analyze validation conditions

    The input is empty and touched, so required error is true, but email error is false because empty triggers required first.
  2. Step 2: Determine which message shows

    Only the required error message is shown because emailControl.errors?.required is true.
  3. Final Answer:

    The message 'Email is required.' will be displayed -> Option C
  4. Quick Check:

    Empty + touched triggers required error = B [OK]
Hint: Empty touched field triggers required error message [OK]
Common Mistakes:
  • Assuming invalid email message shows on empty input
  • Showing no message on touched empty field
  • Showing both messages simultaneously
4. You wrote this Angular code to show a required error:
<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?
medium
A. You need to add Validators.email to the control
B. You forgot to check if the control is touched before showing errors
C. You should use emailControl.invalid instead of emailControl.errors.required
D. You must use ngModel instead of formControl

Solution

  1. Step 1: Understand when errors should show

    Errors should show only after user interaction, so checking touched is important.
  2. Step 2: Identify missing condition

    The code only checks errors.required but not if the user touched the field, so error stays hidden initially.
  3. Final Answer:

    You forgot to check if the control is touched before showing errors -> Option B
  4. Quick Check:

    Check touched before errors to show messages = C [OK]
Hint: Always check touched before showing validation errors [OK]
Common Mistakes:
  • Showing errors without user interaction check
  • Confusing invalid with errors property
  • Adding unrelated validators like email for required error
5. You want to show validation errors for a password field only if the user has interacted with it and it is invalid. You also want to show a custom message if the password is less than 8 characters. Which Angular template snippet correctly implements this?
hard
A.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.invalid">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>
B.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.errors?.minlength">
  <small>Password must be at least 8 characters.</small>
</div>
C.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.touched">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>
D.
<input type="password" [formControl]="passwordControl">
<div *ngIf="passwordControl.invalid && passwordControl.touched">
  <small *ngIf="passwordControl.errors?.minlength">Password must be at least 8 characters.</small>
</div>

Solution

  1. Step 1: Ensure errors show only after user interaction

    Check both invalid and touched to avoid showing errors prematurely.
  2. Step 2: Show specific minlength error message

    Use passwordControl.errors?.minlength inside the conditional block to display the custom message.
  3. Final Answer:

    Correctly combines invalid, touched, and minlength error check -> Option D
  4. Quick Check:

    Check invalid and touched with minlength error = A [OK]
Hint: Combine invalid and touched to show specific error messages [OK]
Common Mistakes:
  • Showing errors without checking touched
  • Showing errors on invalid without user interaction
  • Missing specific error condition for minlength
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard