Sub topic 10
Validate across fields
Some rules depend on more than one field. A password confirmation must equal the password. An end date must come after a start date. This chapter shows how to write those rules in a Signal Forms schema function.
It assumes you can already create a form with form(), add built-in validators, and write a simple custom validator with validate(). Signal Forms is experimental in Angular 21, so names may change in later releases.
Why a single-field validator is not enough
A built-in validator such as minLength() looks only at the field it is attached to. A cross-field rule has to read a second field. Signal Forms handles this inside the schema function. The validator callback receives a context object, and that object includes valueOf(), which reads the current value of any field you pass it by path.
Compare two fields
The following form checks that confirm matches password.
import { signal } from '@angular/core';
import { form, required, minLength, validate } from '@angular/forms/signals';
const model = signal({ password: '', confirm: '' });
const signupForm = form(model, (path) => {
required(path.password);
minLength(path.password, 8, { message: 'Use at least 8 characters' });
validate(path.confirm, ({ value, valueOf }) =>
value() === valueOf(path.password)
? null
: { kind: 'mismatch', message: 'Passwords must match' }
);
});The validator is attached to path.confirm, so these things follow:
value()is the value of the confirm field itself.valueOf(path.password)is the value of the password field.- Returning
nullmeans the field is valid. - Returning an object with a
kindand amessagemeans it is invalid. The error appears insignupForm.confirm().errors().
Because valueOf() reads a signal, the validator depends on the password field. When the user edits the password after typing the confirmation, the rule runs again and the error updates. You do not subscribe to anything or call an update method.
Choose where the error lives
The field you pass as the first argument to validate() is the field that receives the error. For a confirmation rule, put it on confirm. The user is looking at that input, and the existing error display code for confirm().errors() shows it without changes.
For a date range, attach the rule to the later field:
import { signal } from '@angular/core';
import { form, validate } from '@angular/forms/signals';
const trip = signal({ start: '', end: '' });
const tripForm = form(trip, (path) => {
validate(path.end, ({ value, valueOf }) => {
const start = valueOf(path.start);
if (!start || !value()) return null;
return value() >= start
? null
: { kind: 'range', message: 'End date must not be before the start date' };
});
});The rule returns null while either date is empty. Otherwise an empty form would show a range error before the user has typed anything. Leave "is it filled in?" to required() and keep each validator responsible for one thing. ISO date strings (2026-11-26) compare correctly as text.
Common mistakes
- Reading the other field from the model. Calling
model().passwordinside the validator can bypass the dependency tracking thatvalueOf()provides. UsevalueOf(). - Putting the rule on the wrong field. The error shows up where the validator is attached, so check which field you passed.
- Duplicating
required. Returnnullfor empty values and letrequired()report them. - Reusing a
kindfor different problems. Template code often useserror.kindas thetrackkey and to choose messages, so each rule should have its own.
Exercise
A signup form has email and confirmEmail fields. Write the validator that reports a mismatch on confirmEmail, and say what happens if the user later changes email.
Show answer
Inside the schema function, call validate(path.confirmEmail, ({ value, valueOf }) => value() === valueOf(path.email) ? null : { kind: 'emailMismatch', message: 'Emails must match' }). Because valueOf() reads the email field's signal, the validator runs again whenever email changes, so the error on confirmEmail appears or clears without extra code.