Sub topic 18
Disabled Controls and getRawValue
This chapter assumes you can already build a typed FormGroup with FormBuilder, bind it with formControlName, and subscribe to valueChanges. It covers what disabling a control does to its value, its validation, and its parent group. It also covers why form.value and form.getRawValue() can return different objects.
What disabling actually does
You disable a control by calling disable() on it, or by creating it in a disabled state. A disabled control:
- has
statusset to'DISABLED'; - skips its validators, so
errorsisnull; - reports
valid === falseandinvalid === false, because neither state applies; - is left out of its parent's
value; - does not count toward its parent's validity.
The last two points cause most of the bugs. Suppose a required field is disabled while it is empty. Its parent group can still be valid, and the field disappears from form.value.
value vs getRawValue
| Property / method | Includes disabled controls? | Typed return (strict forms) |
|---|---|---|
| `form.value` | No | `Partial<{ ... }>`, so every key is optional |
| `form.getRawValue()` | Yes | Full type, all keys present |
| `control.value` (single control) | Yes, the control keeps its own value | `T` |
A disabled control keeps its own value. Only the parent's aggregation leaves it out. That is why strictly typed forms type form.value as Partial: TypeScript cannot know which controls will be disabled at runtime.
One edge case: if every child of a group is disabled, the group itself becomes disabled. Its value then includes all children, because Angular falls back to reporting everything rather than returning an empty object. Don't depend on this. Call getRawValue() whenever you need the complete data.
A worked example: read-only and conditional fields
This checkout form uses disabled controls for two common cases. The order ID is displayed but never editable. The gift message is editable only when gift wrapping is selected.
import { Component, DestroyRef, OnInit, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-checkout-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<label>
Order
<input formControlName="orderId" />
</label>
<label>
Email
<input type="email" formControlName="email" />
</label>
<label>
<input type="checkbox" formControlName="giftWrap" />
Gift wrap
</label>
<label>
Message
<textarea formControlName="giftMessage"></textarea>
</label>
<p>Form status: {{ form.status }}</p>
<button type="submit" [disabled]="form.invalid">Place order</button>
</form>
`,
})
export class CheckoutFormComponent implements OnInit {
private fb = inject(FormBuilder).nonNullable;
private destroyRef = inject(DestroyRef);
form = this.fb.group({
orderId: [{ value: 'ORD-1042', disabled: true }],
email: ['', [Validators.required, Validators.email]],
giftWrap: [false],
giftMessage: [{ value: '', disabled: true }, [Validators.required, Validators.maxLength(120)]],
});
ngOnInit(): void {
this.form.controls.giftWrap.valueChanges
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((wrap) => {
const message = this.form.controls.giftMessage;
if (wrap) {
message.enable();
} else {
message.disable();
}
});
}
onSubmit(): void {
console.log('value', this.form.value);
console.log('raw', this.form.getRawValue());
}
}The { value, disabled } object is a boxed value, which is how you create a control that starts disabled. giftMessage has Validators.required, but the form stays valid while gift wrap is off, because a disabled control is not validated. When the user ticks the checkbox, enable() runs validation again. The empty message then makes the form invalid.
On submit with gift wrap off and a valid email, the console shows:
value { email: '[email protected]', giftWrap: false }
raw { orderId: 'ORD-1042', email: '[email protected]', giftWrap: false, giftMessage: '' }Decide which one your backend needs. Send value if a disabled field means "not applicable". Send getRawValue() if the server expects every key, including read-only ones such as orderId.
Do not use the disabled attribute
The [disabled] binding is fine on the submit button, which is not a form control. On an input bound with formControlName or [formControl], however, [disabled]="..." sets only the DOM property. The model never learns about it, so the value is still aggregated and still validated. Angular logs a console warning telling you to disable the control in the model instead. The model is the source of truth. The directive pushes the disabled state to the DOM for you, through the value accessor's setDisabledState. A later chapter covers that method for custom controls.
Options on disable() and enable()
Both methods accept { onlySelf?: boolean; emitEvent?: boolean }.
emitEvent: falsestopsvalueChangesandstatusChangesfrom emitting on the control and its ancestors.onlySelf: trueupdates this control without recalculating the parent's value and validity.
emitEvent: false matters most when you disable controls from inside a subscription:
// Infinite loop: disabling changes the group's value, which re-triggers this subscription.
this.form.valueChanges.subscribe(() => {
this.form.controls.giftMessage.disable();
});
// Safe: no events are emitted by the state change.
this.form.valueChanges.subscribe((v) => {
const message = this.form.controls.giftMessage;
if (!v.giftWrap && message.enabled) {
message.disable({ emitEvent: false });
}
});The worked example avoids the problem in a different way. It subscribes to the checkbox control's own valueChanges, and that stream does not emit when a sibling changes.
Interactions with other APIs
setValue/patchValuework on disabled controls. Writing to a read-only field programmatically is allowed.patchValueon a group with a disabled child still updates that child.reset()keeps the current disabled state. To change it during a reset, pass a boxed value:control.reset({ value: '', disabled: false }).markAllAsTouched()marks disabled controls too. Because theirerrorsisnull, error templates that checkhasErrorwill not display anything for them.- Disabling a group disables every descendant. Enabling the group enables every descendant again, including ones you had disabled on purpose. Re-apply the conditional rules after you enable a group.
Exercises
Exercise
A FormGroup has name (required, empty, disabled) and age (valid, enabled). What are form.valid, form.value, and form.controls.name.valid?
Show answer
form.valid is true because the disabled name is excluded from validation. form.value is { age: ... } with no name key. form.controls.name.valid is false, and so is form.controls.name.invalid, because its status is DISABLED.
Exercise
Why is form.value typed as Partial<...> in strictly typed forms while getRawValue() is not?
Show answer
Any control can be disabled at runtime, and disabled controls are left out of value, so TypeScript must treat every key as possibly missing. getRawValue() always includes every control, so it returns the full type.
Exercise
You lock a form during an HTTP save with form.disable() and unlock it with form.enable(). Afterwards, giftMessage is editable even though gift wrap is off. Why, and how do you fix it?
Show answer
Enabling a group enables all descendants, which overrides the earlier giftMessage.disable(). After form.enable(), re-apply the rule, for example by disabling giftMessage when giftWrap.value is false. Moving that rule into a reusable method keeps both code paths consistent.