Sub topic 8
Reading and submitting the value
This chapter assumes you can build a FormArray of FormGroups, bind it with formArrayName and [formGroupName], and add or remove rows. It covers how to read the data back out and send it.
Two ways to read the data
Every control has a value property. For a FormArray, value is an array with one entry per child control. For an array of groups, each entry is an object.
getRawValue() returns the same shape, but it includes disabled controls. value leaves them out. This difference causes most submit bugs with dynamic forms.
| Property | Disabled controls | Typed result for a group in the array |
|---|---|---|
| `value` | Left out | `Partial<{ name: string; quantity: number }>` |
| `getRawValue()` | Included | `{ name: string; quantity: number }` |
What `value` leaves out
Disabling a field removes that key from its group's value. Disabling a whole group removes that group from the array's value. The remaining entries shift down, so index 2 in the form may be index 1 in value.
A submit handler
The component below uses getRawValue(), so a locked field still reaches the server. It also refuses to submit an invalid form.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
interface OrderItem {
name: string;
quantity: number;
}
@Component({
selector: 'app-order-submit',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="form" (ngSubmit)="submit()">
<div formArrayName="items">
@for (item of items.controls; track item; let i = $index) {
<div [formGroupName]="i">
<input formControlName="name" placeholder="Name" />
<input type="number" formControlName="quantity" />
</div>
}
</div>
<button type="button" (click)="lockFirstName()">Lock first name</button>
<button type="submit">Submit</button>
</form>
`,
})
export class OrderSubmitComponent {
private fb = inject(NonNullableFormBuilder);
form = this.fb.group({
items: this.fb.array([
this.fb.group({
name: ['Pen', Validators.required],
quantity: [2, [Validators.required, Validators.min(1)]],
}),
]),
});
get items() {
return this.form.controls.items;
}
lockFirstName() {
this.items.at(0).controls.name.disable();
}
submit() {
if (this.form.invalid) {
this.form.markAllAsTouched();
return;
}
const payload: { items: OrderItem[] } = this.form.getRawValue();
console.log('value:', this.form.value);
console.log('raw:', payload);
}
}Click "Lock first name", then submit. The value log shows { items: [{ quantity: 2 }] }. The raw log shows { items: [{ name: 'Pen', quantity: 2 }] }.
Common mistakes
- Posting
valuewhen fields are disabled. UsegetRawValue(), or check that no control is disabled. - Skipping the validity check. A submit button without
[disabled]can still be triggered by pressing Enter, so testform.invalidin the handler too. - Not showing errors on untouched rows. Call
markAllAsTouched()so every row displays its messages. - Assuming indexes match. If you disable whole groups,
valueindexes no longer line up withcontrols. UsegetRawValue()when you need to map entries back to rows. - Mutating the result. Both properties return fresh structures, but treat them as read-only and build the request body in one place.
With NonNullableFormBuilder, getRawValue() gives complete types, so you can assign it straight to an interface as shown above. value makes every group field optional, which forces extra checks.
Exercise
A form has an items array with three groups. The second group is disabled with items.at(1).disable(). How many entries does form.value.items have, and how many does form.getRawValue().items have?
Show answer
form.value.items has 2 entries, because the disabled group is left out and the third group moves to index 1. form.getRawValue().items has 3 entries, because it includes disabled controls.