Sub topic 5
Read field state as signals
This chapter assumes you know signal(), computed() and effect(). It also assumes you have built a form with form() and bound it to inputs with [field], as in the previous chapters. Signal Forms is experimental in Angular 21, so names and details can change in later releases.
Two calls: the field tree and the field state
A form created with form() is a tree of fields that mirrors the shape of your model. Each node in the tree is a function. Calling the function returns a field state, an object whose properties are signals.
const model = signal({ email: '', password: '' });
const loginForm = form(model);
loginForm.email; // the field (a function, not a value)
loginForm.email(); // the field state (an object of signals)
loginForm.email().value(); // the current value (a string)Most beginner mistakes come from skipping one of these calls. The first access (loginForm.email) navigates to a child field. The second call (loginForm.email()) gives you the state. The third call reads one signal from that state. Because every property is a signal, templates, computed() and effect() track them automatically.
The main state signals
The signals you will use most often are listed below.
| Signal | Type | Meaning |
|---|---|---|
| `value` | writable signal | The current value of this field. For a group, it is the object of child values. |
| `valid` | boolean signal | No errors, and no validation still running. |
| `invalid` | boolean signal | At least one error is present. |
| `pending` | boolean signal | An asynchronous validator has not finished yet. |
| `errors` | signal of an array | The error objects for this field. Each has a `kind` and usually a `message`. |
| `touched` | boolean signal | The user has interacted with the field and left it (blur). |
| `dirty` | boolean signal | The user has changed the value. |
| `disabled`, `hidden`, `readonly` | boolean signals | Conditional state, covered in a later chapter. |
The model signal and value stay in sync. When the user types, value() and the model both update. If you call model.set(...), the field reflects the new value. value is also writable, so loginForm.email().value.set('[email protected]') updates the model.
Valid, invalid and pending are not opposites
It is tempting to treat valid() and invalid() as the negation of each other. They are not, because of asynchronous validation. While an async validator runs, the field is pending. In that state it is neither confirmed valid nor known invalid.
| Situation | `valid()` | `invalid()` | `pending()` |
|---|---|---|---|
| All validators pass | true | false | false |
| A synchronous validator fails | false | true | false |
| Async check still running, no errors so far | false | false | true |
Pick the signal that matches what you want to do:
- Use
invalid()to decide whether to show an error. It is only true when there is something to show. - Use
valid()to decide whether it is safe to proceed. It stays false until pending work finishes. - Use
pending()to show a spinner or a "Checking..." label.
Touched versus dirty
These two signals answer different questions.
touched()answers "has the user been here?" It becomes true when the field loses focus.dirty()answers "has the user changed it?" It becomes true after the user edits the value.
A field can be touched but not dirty (the user tabbed through it). Dirty behavior also depends on how the change happened. In Angular 21, a user edit through the [field] binding marks the field dirty. Assigning to the model in code, for example to prefill a form after a fetch, is a programmatic change and does not mark it dirty. Check this against your Angular version if your logic depends on it.
Use touched() to decide when to show errors, so users are not scolded before they finish typing. Use dirty() for things like an "unsaved changes" warning.
Aggregation: the form itself is a field
The root of the tree is also a field, so it has the same state signals. loginForm() gives the state of the whole form. Its valid() and invalid() reflect the fields beneath it. A group is invalid if any descendant is invalid, and the same holds for touched() and dirty() (any descendant). That is why the first chapter's login example can write:
<button type="submit" [disabled]="loginForm().invalid()">Log in</button>A group field also has its own errors(), which holds only the errors attached to that node. It does not include its children's errors, so do not use loginForm().errors() to list every message on the form. To show per-field messages, read each field's errors().
A worked example: a live state panel
The following component shows each state signal for one field, plus a derived signal built with computed(). It is a useful debugging tool while you learn.
import { Component, computed, signal } from '@angular/core';
import { form, Field, required, email } from '@angular/forms/signals';
@Component({
selector: 'app-state-panel',
imports: [Field],
template: `
<label>Email <input type="email" [field]="contactForm.email" /></label>
<ul>
<li>value: {{ contactForm.email().value() }}</li>
<li>valid: {{ contactForm.email().valid() }}</li>
<li>invalid: {{ contactForm.email().invalid() }}</li>
<li>touched: {{ contactForm.email().touched() }}</li>
<li>dirty: {{ contactForm.email().dirty() }}</li>
<li>errors: {{ errorKinds() }}</li>
</ul>
@if (showError()) {
<p class="error">{{ contactForm.email().errors()[0].message }}</p>
}
`,
})
export class StatePanelComponent {
private readonly model = signal({ email: '' });
protected readonly contactForm = form(this.model, (path) => {
required(path.email, { message: 'Email is required' });
email(path.email, { message: 'Enter a valid email address' });
});
protected readonly errorKinds = computed(() =>
this.contactForm
.email()
.errors()
.map((e) => e.kind)
.join(', ') || 'none'
);
protected readonly showError = computed(() => {
const state = this.contactForm.email();
return state.touched() && state.invalid();
});
}Run it and watch the panel. On load, invalid is already true, because the empty value fails required, but showError() is false because the field is untouched. Type a character, and dirty becomes true while errors switches to the email-format error. Click elsewhere, and touched becomes true, so the message appears.
The showError computed signal shows the usual pattern. Combine touched() with invalid() to get a reliable "show the error now" condition. Both signals are read inside computed(), so it recalculates only when one of them changes.
Reading state in effects and computed values
Because state is made of signals, you can derive anything from it without subscriptions.
import { computed, effect, signal } from '@angular/core';
import { form, required } from '@angular/forms/signals';
const model = signal({ name: '', city: '' });
const addressForm = form(model, (path) => {
required(path.name);
required(path.city);
});
const canSave = computed(
() => addressForm().valid() && addressForm().dirty()
);
effect(() => {
if (addressForm().dirty()) {
console.log('Unsaved changes:', addressForm().value());
}
});Run effect() inside an injection context, such as a component constructor or a field initializer, as with any Angular effect. The snippet above is shown at top level for brevity.
Common errors
- Forgetting a call.
loginForm.email().invalidwithout the trailing()is the signal function, which is always truthy in a condition. The type checker usually catches this in templates with strict settings. Make sure that strict template checking is on. - Reading the wrong level.
loginForm().errors()does not contain child errors. Read the child field instead. - Showing errors too early. Testing only
invalid()displays errors on a pristine form. Addtouched(). - Treating
!valid()as "has errors". During async validation it is false for bothvalid()andinvalid(). Useinvalid()for error display andpending()for waiting state. - Expecting dirty after a programmatic change. Setting the model in code does not mark the field dirty. Do not use
dirty()to detect that the model differs from its initial value. - Assuming disabled fields report errors. In Angular 21, disabled and hidden fields are excluded from validation, so they do not hold errors and do not block the form from being valid.
Exercise
A password field has a synchronous required validator and an asynchronous "not previously breached" check. The user has typed a value that passes required, and the async request is still running. What are valid(), invalid() and pending() for that field, and which signal should you use to decide whether to show an error message?
Show answer
valid() is false, invalid() is false and pending() is true. The field has no errors yet but is not confirmed valid. Use invalid() (combined with touched()) to decide whether to show an error, because it only becomes true once an error exists. Use pending() to show a "Checking..." indicator.
Exercise
Write a computed() signal named showEmailError for contactForm.email() that is true only when the field has been touched and is invalid. Why is invalid() alone a poor condition for showing an error?
Show answer
const showEmailError = computed(() => { const s = contactForm.email(); return s.touched() && s.invalid(); }); Using invalid() alone shows the error immediately on a pristine form. A required field is invalid from the start, so the user would see an error before typing anything.
Exercise
A teammate writes @if (loginForm().errors().length) { ... } to list all validation messages for the form, and it never shows anything even though the email field is invalid. What is wrong, and how do you fix it?
Show answer
loginForm().errors() returns only errors attached to the root node, not those of child fields. Read each field's own errors instead, for example loginForm.email().errors() and loginForm.password().errors(), and render them separately.