Sub topic 7
Show validation errors to the user
This chapter assumes you can write a standalone Angular component, use signal(), and have a form built with form() and bound with [field], as covered in earlier chapters. It also assumes you have added built-in validators in a schema function.
What a field exposes
Validators only record problems. Your template decides whether and when to show them. Each field gives you signals for this:
errors()returns an array of error objects. Each has akind(such asrequired) and usually amessage.invalid()is true when the field has at least one error.touched()is true after the user has focused the field and left it.dirty()is true after the user has changed the value.
The message comes from the options you passed to the validator, for example required(path.email, { message: 'Email is required' }). If you omit it, message is undefined, so supply one for every error you plan to show.
Decide when to show errors
An empty required field is invalid from the first render. If you show errors immediately, the user sees red text before typing anything. The usual rule is to show errors only when the field is both touched() and invalid().
Render the errors
Read the field's state by calling the field, then call the signal you need. Loop over errors() with @for, and use kind as the track key.
import { Component, signal } from '@angular/core';
import { form, Field, required, email } from '@angular/forms/signals';
@Component({
selector: 'app-newsletter',
imports: [Field],
template: `
<label>
Email
<input type="email" [field]="newsletterForm.email" />
</label>
@if (newsletterForm.email().touched() && newsletterForm.email().invalid()) {
<ul class="errors">
@for (error of newsletterForm.email().errors(); track error.kind) {
<li>{{ error.message }}</li>
}
</ul>
}
`,
})
export class NewsletterComponent {
private readonly model = signal({ email: '' });
protected readonly newsletterForm = form(this.model, (path) => {
required(path.email, { message: 'Email is required' });
email(path.email, { message: 'Enter a valid email address' });
});
}An empty field fails both validators, so the list shows two messages at once. Often you want only the first one. Replace the @for with the first item, for example {{ newsletterForm.email().errors()[0].message }}. This is safe inside the @if, because invalid() guarantees at least one error.
Avoid repeating the markup
With many fields, copying this block gets tedious. Put it in a small component that takes the field state as an input. Because errors() and touched() are signals, the component updates by itself when the user types.
Common mistakes
- Showing errors on a pristine form. If you skip the
touched()check, every field shows an error on load. - Using
dirty()instead oftouched(). A user who tabs through a required field never changes it, sodirty()stays false and no error appears.touched()handles that case. - Forgetting the call parentheses.
loginForm.email.invalid()is wrong. The field is a function, so writeloginForm.email().invalid(). - Tracking by index. Track by
error.kindso Angular can reuse DOM nodes when the list changes.
Exercise
A user tabs through a required field without typing, and no error appears. The template shows errors when dirty() && invalid(). What is wrong, and what should the condition be?
Show answer
dirty() becomes true only when the value changes, so it stays false for a field the user skipped. Use touched() && invalid(), because touched() becomes true once the user leaves the field.