Sub topic 14
Model repeated fields with arrays
This chapter assumes you can already create a form with form(), bind inputs with [field], and add validators in a schema function. Here you will model a list of repeated groups, such as invoice lines, and validate every item with one rule set.
Put the array in the model
In Signal Forms the model is the source of truth, so a repeating group is an array property on the model. The form structure follows the data. You don't create or remove field objects. You change the array, and the form updates to match.
interface Line {
description: string;
quantity: number;
}
interface Invoice {
customer: string;
lines: Line[];
}form() gives you a field tree that mirrors this shape. invoiceForm.lines is iterable, and each element exposes description and quantity as child fields.
Validate every item with applyEach
Writing required(path.lines[0].description) would only cover the first item. Use applyEach() to apply a schema to every element, including elements added later.
import { Component, signal } from '@angular/core';
import { form, Field, required, min, applyEach } from '@angular/forms/signals';
interface Line {
description: string;
quantity: number;
}
interface Invoice {
customer: string;
lines: Line[];
}
@Component({
selector: 'app-invoice',
imports: [Field],
template: `
<label>Customer <input [field]="invoiceForm.customer" /></label>
@for (line of invoiceForm.lines; track line; let i = $index) {
<fieldset>
<input placeholder="Description" [field]="line.description" />
<input type="number" [field]="line.quantity" />
@if (line.description().touched() && line.description().invalid()) {
<p class="error">{{ line.description().errors()[0].message }}</p>
}
<button type="button" (click)="removeLine(i)">Remove</button>
</fieldset>
}
<button type="button" (click)="addLine()">Add line</button>
<button type="submit" [disabled]="invoiceForm().invalid()">Save</button>
`,
})
export class InvoiceComponent {
private readonly model = signal<Invoice>({
customer: '',
lines: [{ description: '', quantity: 1 }],
});
protected readonly invoiceForm = form(this.model, (path) => {
required(path.customer, { message: 'Customer is required' });
applyEach(path.lines, (line) => {
required(line.description, { message: 'Describe this line' });
min(line.quantity, 1, { message: 'Quantity must be at least 1' });
});
});
protected addLine() {
this.model.update((m) => ({
...m,
lines: [...m.lines, { description: '', quantity: 1 }],
}));
}
protected removeLine(index: number) {
this.model.update((m) => ({
...m,
lines: m.lines.filter((_, i) => i !== index),
}));
}
}The callback you pass to applyEach() receives a path for one item, not the whole array. Each item gets its own state, so one empty description marks only that row as invalid.
Add and remove items
Add and remove items by writing a new array to the model signal. Create new arrays and objects instead of mutating the old ones, as addLine() and removeLine() do. The form reads the model through signals, so a mutation that doesn't produce a new value may not be noticed.
Common errors
- Using an index in the schema.
path.lines[0]is fixed to the first item. UseapplyEach()instead. - Mutating with
push(). The signal's value is not replaced, so dependents may not update. - Tracking by
$index. After removing a middle row, state can appear on the wrong row. Track by the field itself, as intrack line.
Exercise
A user removes the second of three invoice lines. Which two things must you do for the form to reflect it, and what must you not do?
Show answer
Write a new array without that item to the model signal, for example with filter() inside model.update(). The form then rebuilds its item fields, and the applyEach rules run for the remaining items. Do not splice the existing array in place and do not create or delete field objects by hand, because the model is the source of truth.