Sub topic 11
Disable, hide, or make fields read-only conditionally
This chapter assumes you can build a form with form(), bind inputs with [field], and read field state as signals. It also assumes you have seen built-in validators and the when option. Signal Forms is experimental in Angular 21, so check the names below against your installed version.
Three kinds of conditional state
Every field has three state flags that a schema function can control:
| State | Rule function | Field state signal | What the `[field]` directive does |
|---|---|---|---|
| Disabled | `disabled()` | `disabled()` | Sets the native `disabled` property on the input |
| Read-only | `readonly()` | `readonly()` | Sets the native `readonly` property on the input |
| Hidden | `hidden()` | `hidden()` | Nothing. Your template decides what to render |
The difference in the last column matters. Disabled and read-only are mirrored onto the input by the binding. Hidden is only a flag. A hidden field stays in the DOM unless your template removes it.
All three rules take the same arguments: a field path and a logic function. The logic function receives a context object. Its most useful members are value() for the current field and valueOf(path) for any other field in the form. Each rule's logic function is reactive. Angular re-runs it when a signal it reads changes, so you never subscribe to anything or call updateValueAndValidity().
Disabling a field from another field's value
The logic function returns true when the field should be disabled. This version of the order form disables the coupon input until the user ticks a box:
import { Component, signal } from '@angular/core';
import { form, Field, required, disabled } 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>
<p>Coupon disabled: {{ orderForm.coupon().disabled() }}</p>
`,
})
export class OrderComponent {
private readonly model = signal({ hasCoupon: false, coupon: '' });
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',
});
});
}The disabled() rule reads hasCoupon through valueOf(). Because valueOf() reads a signal, the rule depends on that field automatically. The when option on required() does a similar job for validation: the rule runs only while the coupon is in use.
Giving a reason
The logic function can return a string instead of true. A non-empty string means the field is disabled, and the string is the reason:
disabled(path.coupon, ({ valueOf }) =>
valueOf(path.hasCoupon) ? false : 'Tick "I have a coupon" first',
);The field state then exposes disabledReasons(), a signal holding the reasons from every active disabled rule. Show them next to the input so users learn why they cannot type.
@for (reason of orderForm.coupon().disabledReasons(); track $index) {
<small>{{ reason.message }}</small>
}Read-only fields
A read-only field shows its value and can be focused and selected, but not edited. Use it for values that are final once something else has happened, such as an email address after verification:
import { signal } from '@angular/core';
import { form, readonly } from '@angular/forms/signals';
const model = signal({ email: '[email protected]', emailVerified: true });
const accountForm = form(model, (path) => {
readonly(path.email, ({ valueOf }) => valueOf(path.emailVerified));
});Choose between the two states by what the user should be able to do:
- Use disabled when the field does not apply right now. Browsers skip disabled inputs in tab order and in native form submission.
- Use read-only when the value applies but must not change. Read-only inputs stay focusable, and screen reader users can still reach them.
Hiding fields
hidden() follows the same pattern, but you must act on the flag yourself:
import { Component, signal } from '@angular/core';
import { form, Field, hidden, required } from '@angular/forms/signals';
@Component({
selector: 'app-shipping',
imports: [Field],
template: `
<label>
<input type="checkbox" [field]="shippingForm.sameAsBilling" />
Ship to billing address
</label>
@if (!shippingForm.shippingStreet().hidden()) {
<label>Shipping street <input [field]="shippingForm.shippingStreet" /></label>
@if (shippingForm.shippingStreet().touched() && shippingForm.shippingStreet().invalid()) {
<p class="error">Street is required</p>
}
}
<button type="button" [disabled]="shippingForm().invalid()">Continue</button>
`,
})
export class ShippingComponent {
private readonly model = signal({
sameAsBilling: true,
billingStreet: '12 Main St',
shippingStreet: '',
});
protected readonly shippingForm = form(this.model, (path) => {
hidden(path.shippingStreet, ({ valueOf }) => valueOf(path.sameAsBilling));
required(path.shippingStreet, { message: 'Street is required' });
});
}While the checkbox is ticked, shippingStreet is hidden, and the @if removes its input. The Continue button still enables, because a hidden field does not count against validity.
What hidden, disabled, and read-only do to validation
Fields in any of these three states are not validated. They report no errors, and they do not make the parent form invalid. That is why the required() rule above does not block submission while the street is hidden. It is also why you rarely need a when option on a validator for a field that already has a matching hidden() or disabled() rule.
Common pitfalls
The value stays in the model. Hiding or disabling a field does not clear it. In the shipping example, model().shippingStreet keeps whatever the user typed before ticking the box. If your API must not receive stale data, clear the value yourself or build the payload from only the active fields.
protected onSubmit() {
const { sameAsBilling, billingStreet, shippingStreet } = this.model();
const payload = {
billingStreet,
shippingStreet: sameAsBilling ? billingStreet : shippingStreet,
};
console.log(payload);
}Reading state of a conditional field in a template that is itself conditional can lead to confusing output. Check hidden() first, then the other flags.
Rules combine. If several disabled() rules target the same field, the field is disabled when any of them returns a truthy value. You cannot "undo" a disabled rule from a second rule, so put the full condition in one function when it gets complicated.
Applying a whole block of rules conditionally
When many rules share one condition, repeating when on each gets noisy. The applyWhen() function applies a group of rules only while a condition holds:
import { signal } from '@angular/core';
import { form, applyWhen, required, minLength } from '@angular/forms/signals';
const model = signal({ isCompany: false, companyName: '', vatId: '' });
const billingForm = form(model, (path) => {
applyWhen(
path,
({ valueOf }) => valueOf(path.isCompany),
(company) => {
required(company.companyName, { message: 'Company name is required' });
minLength(company.vatId, 8, { message: 'VAT ID looks too short' });
},
);
});Inside the callback, use the path argument it receives (company here), not the outer path. That keeps the rules attached to the conditional scope.
Choosing the right tool
- The field cannot be edited right now, and the user should know why:
disabled()with a reason string. - The field must show a value that is locked:
readonly(). - The field is irrelevant and should disappear:
hidden()plus an@if. - Only the validation depends on a condition: the
whenoption on a single validator, orapplyWhen()for a group.
Exercise
A signup form has accountType ('personal' or 'business') and taxId. The tax ID input should disappear for personal accounts, and a business account must supply a tax ID. A teammate wrote required(path.taxId) and hidden(path.taxId, ({ valueOf }) => valueOf(path.accountType) === 'personal'), but the input still shows for personal accounts. What is missing, and does the required() rule block submission for personal accounts?
Show answer
The template needs @if (!form.taxId().hidden()) { ... } around the input, because hidden() only sets a flag and the [field] directive does not remove the element. The required() rule does not block submission for personal accounts, because hidden fields are not validated and do not make the form invalid. Any text the user typed into taxId earlier stays in the model, so clear it or leave it out of the payload.