
Why form.value Is Partial: Disabled Controls and getRawValue()
- كاتب موثّق
- Angular 22
محتويات المقال
PUT body: {"name":"Mona Adel"}, no email at all. The save handler sends this.form.value as Employee, and the cast was added months earlier to make a TypeScript error go away. That error was the bug, reported before it shipped.
The common misconception
form.value is the form's data. Every control you declared in the FormGroup shows up in it, so it's the payload to send to the API as is.
form.value has every key, and logging it during development confirms the shape you expect. Even the examples in Angular's reactive forms guide pass this.form.value straight to a save call, because nothing in them is disabled. And for years before Angular 14, when reactive forms became strictly typed, most form APIs had any in their types, so nothing hinted that a key could go missing. When the type finally appeared, many teams read it as a quirk of the typings and cast it away.
What actually happens
value collects the enabled controls
value from its children, and the FormGroup API docs are explicit about disabled children: they're excluded from the aggregate value of their ancestors. Disable the email control and form.value stops having an email key. Not null, not an empty string: the key isn't there. A FormArray follows the same rule: its value is an array of its enabled controls, so disabling the second of three rows gives you two elements, and every index after the disabled row shifts.
login.value is Partial<{ email: string; password: string }>, so each member might be undefined. TypeScript can't know which controls you'll disable, so it makes you handle all of them. That's precisely the error the as Employee cast silenced.
getRawValue() collects everything
getRawValue() is the aggregate value including disabled controls, and its type is the full object with no Partial. It's the method the typed forms guide points to when you want every field and don't want to handle undefined.
value wrong. If your endpoint is a PATCH that should receive only what the user was allowed to edit, leaving the disabled controls out is exactly what you want. The two methods answer different questions, and the types tell you which one you asked.
import { Component, inject } from '@angular/core';import { NonNullableFormBuilder, ReactiveFormsModule } from '@angular/forms';@Component({ selector: 'app-edit-employee', imports: [ReactiveFormsModule], templateUrl: './edit-employee.html',})export class EditEmployee { private readonly api = inject(EmployeeApi); private readonly fb = inject(NonNullableFormBuilder); readonly form = this.fb.group({ name: '', email: { value: '', disabled: true }, }); save() { // The cast hides Partial: the PUT body has no email this.api.update(this.form.value as Employee).subscribe(); }}import { Component, inject } from '@angular/core';import { NonNullableFormBuilder, ReactiveFormsModule } from '@angular/forms';@Component({ selector: 'app-edit-employee', imports: [ReactiveFormsModule], templateUrl: './edit-employee.html',})export class EditEmployee { private readonly api = inject(EmployeeApi); private readonly fb = inject(NonNullableFormBuilder); readonly form = this.fb.group({ name: '', email: { value: '', disabled: true }, }); save() { // { name: string; email: string }, disabled controls included const employee: Employee = this.form.getRawValue(); this.api.update(employee).subscribe(); }}getRawValue() already has the shape of Employee, so the compiler checks it.
Nullable controls are a second gap
getRawValue() fills in the keys, but it doesn't remove null. A plain new FormControl('') is typed FormControl<string | null>, because calling reset() sets it to null. Created with { nonNullable: true }, a control resets to its initial value instead, and its type drops the null. NonNullableFormBuilder, used above, applies that option to every control it creates, which is why getRawValue() here matches Employee exactly.
Disabled also means unvalidated
required validator on a disabled, empty field won't make the form invalid, so form.valid can be true while the data is incomplete. And one detail surprises people: when the group itself is disabled, for example while a save is in flight, its value includes all of its controls again.
import { FormControl, FormGroup } from '@angular/forms';const form = new FormGroup({ name: new FormControl('Mona'), email: new FormControl({ value: 'mona@example.com', disabled: true }),});console.log(form.value); // { name: 'Mona' }form.disable();console.log(form.value); // { name: 'Mona', email: 'mona@example.com' }form.value | form.getRawValue() | |
|---|---|---|
Partial<{ ... }> | ||
How to apply it in your project
Search for casts on form values Find .value asand everyform.valuepassed to an HTTP call. Where the form has disabled controls, switch togetRawValue(), or keepvalueand write down why the disabled fields must not be sent.Make controls non-nullable Create forms with NonNullableFormBuilderor{ nonNullable: true }, sogetRawValue()matches your API types withoutnullchecks or casts.Don't trust form.valid for disabled fields Disabled controls aren't validated. If a disabled field must still be correct, check it before saving, and keep the server's validation as the final word. Test the disabled path Add a test that disables the conditional controls, submits, and asserts on the request body, so a missing key fails in CI instead of in production.
Key takeaways
In Reactive Forms, form.valuecontains only enabled controls, which is why its type is aPartial.getRawValue()returns every control, disabled ones included, with a type that matches the full model.Non-nullable controls remove the remaining nullfrom the type; disabled controls also skip validation.Signal Forms keep every field in your model signal, so the omission becomes your decision.
التعليقات (0)