Sub topic 6
Add built-in validators
This chapter assumes you know TypeScript, Angular standalone components, and signals (signal(), computed()). You should also have a form created with form() from a signal model. Signal Forms is experimental in Angular 21, so names and options may change in later releases.
Where validators live
In Signal Forms you do not attach validators to individual controls. You declare them in a schema function, which is the second argument to form(). The function receives a path object that mirrors the shape of your model. Each validator takes a path to a field, plus optional settings.
import { signal } from '@angular/core';
import { form, required, email, minLength } from '@angular/forms/signals';
const model = signal({ email: '', password: '' });
const loginForm = form(model, (path) => {
required(path.email, { message: 'Email is required' });
email(path.email, { message: 'Enter a valid email address' });
required(path.password, { message: 'Password is required' });
minLength(path.password, 8, { message: 'Use at least 8 characters' });
});The schema runs once, when the form is created. After that, every validator re-evaluates automatically when the signals it reads change.
The built-in validators
All of these are imported from @angular/forms/signals.
| Validator | Checks | Example |
|---|---|---|
| `required` | The value is not empty | `required(path.name)` |
| `email` | The value looks like an email address | `email(path.email)` |
| `minLength` | A string or array has at least N items | `minLength(path.password, 8)` |
| `maxLength` | A string or array has at most N items | `maxLength(path.bio, 200)` |
| `min` | A number is at least N | `min(path.age, 18)` |
| `max` | A number is at most N | `max(path.age, 120)` |
| `pattern` | The value matches a regular expression | `pattern(path.zip, /^\d{5}$/)` |
Messages and error kinds
Every validator accepts an options object. The message option sets the text you show to the user. Each error also has a kind, such as required or minLength. Use kind in templates and tests, and use message for display.
If you leave out message, the error still exists and the field is still invalid. Your template then has to supply its own text based on kind.
Running a validator only sometimes
The when option limits a validator to cases where a condition is true. The function receives a context with valueOf(), which reads another field's value.
import { signal } from '@angular/core';
import { form, required } from '@angular/forms/signals';
const model = signal({ hasCoupon: false, coupon: '' });
const orderForm = form(model, (path) => {
required(path.coupon, {
when: ({ valueOf }) => valueOf(path.hasCoupon),
message: 'Enter your coupon code',
});
});While hasCoupon is false, the required rule is skipped, so an empty coupon is valid.
How validators combine
You can apply several validators to one field. They all run, and the field's errors() signal holds every failure. The field is invalid() if at least one error exists.
Common mistakes
- Expecting
requiredto catch whitespace. It checks for an empty value, not for a string of spaces. Usepatternif you need to reject blanks. - Using
minLengthto require a value. An empty string is usually treated as "no value", so pair it withrequiredwhen the field is mandatory. - Forgetting that errors are signals. Call
loginForm.email().errors(), notloginForm.email.errors. - Showing errors too early. A field is invalid before the user touches it. Check
touched()before you display messages.
Exercise
A coupon field must be required only when a checkbox hasCoupon is checked. Which option on required() does this, and how do you read the checkbox value?
Show answer
Use the when option, for example required(path.coupon, { when: ({ valueOf }) => valueOf(path.hasCoupon) }). The valueOf() function in the context reads another field's current value.
Exercise
A password field has required and minLength(8) applied, and the user types "abc". Which errors appear in errors(), and is the field valid?
Show answer
Only the minLength error appears, because the value is not empty. The field is invalid because errors() is not empty.