Sub topic 11
Disable, hide, or make fields read-only conditionally
This chapter assumes you can write a signal model, call form() with a schema function, and bind inputs with [field]. It also assumes you know how to read field state as signals.
Three kinds of conditional state
Some fields only matter in certain situations. A coupon input is useless until the user says they have a coupon. Signal Forms provides three schema rules for this, one for each field state:
| Rule | Field state signal | Meaning |
|---|---|---|
| `disabled()` | `disabled()` | The user cannot edit the field. |
| `readonly()` | `readonly()` | The user can see and select the value but cannot change it. |
| `hidden()` | `hidden()` | The field should not be shown. |
Each rule takes a path and a logic function. The logic function returns true when the state applies. Inside it, valueOf(path.otherField) reads another field's current value. Because the function reads signals, the state updates when those values change.
Disable a field from another field's value
The next example is a complete component. The coupon field is disabled until the checkbox is ticked.
import { Component, signal } from '@angular/core';
import { form, Field, required, disabled, hidden, readonly } from '@angular/forms/signals';
@Component({
selector: 'app-order',
imports: [Field],
template: `
<label><input type="checkbox" [field]="orderForm.hasCoupon" /> I have a coupon</label>
<label>Coupon <input [field]="orderForm.coupon" /></label>
@if (!orderForm.note().hidden()) {
<label>Note <input [field]="orderForm.note" /></label>
}
<label>Total <input [field]="orderForm.total" /></label>
`,
})
export class OrderComponent {
private readonly model = signal({
hasCoupon: false,
coupon: '',
note: '',
total: '10.00',
locked: false,
});
protected readonly orderForm = form(this.model, (path) => {
disabled(path.coupon, ({ valueOf }) => !valueOf(path.hasCoupon));
required(path.coupon, {
when: ({ valueOf }) => valueOf(path.hasCoupon),
message: 'Enter your coupon code',
});
hidden(path.note, ({ valueOf }) => valueOf(path.total) === '');
readonly(path.total, ({ valueOf }) => valueOf(path.locked));
});
}The [field] directive copies disabled and readonly onto the native input for you. It does not remove a hidden field from the page. You decide what hidden means by wrapping the input in @if, as the note field does here.
Why the order of rules matters little
Rules in a schema are declarative. You describe the condition once, and the form re-evaluates it whenever its inputs change. You do not subscribe to changes or call enable() and disable() by hand, as you would in Reactive Forms.
Do not use the empty block above. The diagram follows.
Effect on validation
Fields that are disabled, hidden, or read-only are not interactive, so their validators do not run. A disabled coupon with a required() rule will not make the form invalid. This is why the example above works even without when. The when option on required() is still useful for a field that stays enabled but only needs a value in some cases.
Common mistakes
- Using
hidden()and expecting the input to disappear. You must readhidden()in the template. - Reading the form's state in the logic function instead of using
valueOf(). UsevalueOf(path.x)so the rule depends on the right signal. - Forgetting that these APIs are experimental in Angular 21, so names may change.
Exercise
A user ticks "I have a coupon", types a code, then unticks the box. The coupon field is now disabled. What happens to the typed code in the model, and does required() block submission?
Show answer
The typed code stays in the model, because disabling a field does not clear its value. required() does not block submission, because validators do not run on a disabled field. If you do not want the stale code sent, clear it in your submit handler or remove it from the payload.