Sub topic 13
Validate asynchronously
This chapter assumes you know how to write synchronous validators in a schema function and how to read field state signals. You should also know Angular's resource().
When you need it
Some rules cannot be decided in the browser. "Is this username already taken?" requires a server call. Synchronous validators return an answer immediately. An asynchronous validator starts a request, and the field has no verdict until the request finishes.
Signal Forms handles this with validateAsync(). It builds on resource(), so the request tracks signals and cancels stale work when the value changes.
The shape of validateAsync()
validateAsync() takes a field path and an options object with four parts:
| Option | Purpose |
|---|---|
| `params` | Reads the field and returns the input for the request. Returning `undefined` skips the request. |
| `factory` | Receives the params signal and returns a `resource()`. |
| `onSuccess` | Turns the resource result into an error object, or `undefined` if the value is fine. |
| `onError` | Handles a failed request. |
import { resource, signal } from '@angular/core';
import { form, required, minLength, validateAsync } from '@angular/forms/signals';
const model = signal({ username: '' });
const signupForm = form(model, (path) => {
required(path.username, { message: 'Choose a username' });
minLength(path.username, 3, { message: 'Use at least 3 characters' });
validateAsync(path.username, {
params: ({ value }) => (value() === '' ? undefined : value()),
factory: (params) =>
resource({
params,
loader: async ({ params: name }) => {
const response = await fetch(
`/api/usernames/${encodeURIComponent(name)}`
);
const body: { taken: boolean } = await response.json();
return body.taken;
},
}),
onSuccess: (taken) =>
taken ? { kind: 'taken', message: 'That username is already in use' } : undefined,
onError: () => ({ kind: 'lookup', message: 'Could not check the username' }),
});
});The params function returns undefined for an empty field, so no request runs. The loader reads its input from the resource's own params, which factory received.
The pending state
While a request is running, the field is neither valid nor invalid. Read pending() on the field state to show progress:
@if (signupForm.username().pending()) {
<p>Checking availability...</p>
}The field moves through these states:
Async errors appear in the same errors() list as synchronous ones, so your existing error display needs no changes.
Common mistakes
- Firing on every keystroke. Each change to the value starts a new request. The resource cancels the previous one, but the server still receives traffic. Skip short or empty values in
params, and consider debouncing for expensive checks. - Treating a failed request as a pass. If
onErrorreturnsundefined, a network failure lets the user through. Decide deliberately whether the form should block in that case. - Trusting the client check. The server must repeat the rule when you submit. Another user can claim the name between the check and the save.
- Disabling submit only on
invalid(). A pending field is not yet invalid. Also checkpending(), or the user can submit before the check completes.
Exercise
A user types a username, then clicks "Sign up" while the availability check is still running. Which field state tells you the result is not known yet, and what should the submit button do?
Show answer
The field's pending() signal is true. The button should stay disabled, for example with [disabled]="signupForm().invalid() || signupForm.username().pending()", because invalid() alone does not cover a check that has not finished.