Sub topic 4
Creating a FormArray with FormBuilder
This chapter assumes you know TypeScript, standalone Angular components, and the basics of FormControl, FormGroup and Validators. It also assumes you have seen a plain new FormArray([...]).
Why use FormBuilder for arrays
Calling new FormControl(...), new FormGroup(...) and new FormArray(...) works, but nested forms become long and repetitive. FormBuilder is an injectable service with factory methods (control, group, array) that create the same objects with less code.
For typed forms, inject NonNullableFormBuilder. Its controls reset to their initial value instead of null, so a control's type is string rather than string | null.
The array method
fb.array() takes two arguments: an array of child controls, and optionally a validator or a list of validators for the array itself.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-email-list',
standalone: true,
template: '',
})
export class EmailListComponent {
private fb = inject(NonNullableFormBuilder);
emails = this.fb.array(
[
this.fb.control('[email protected]', Validators.email),
this.fb.control('[email protected]', Validators.email),
],
Validators.minLength(1),
);
}emails has the type FormArray<FormControl<string>>. Its value is ['[email protected]', '[email protected]'].
Validators.minLength(1) works on the array because it checks the length of the control's value, and an array value has a length. The check runs on the array, not on each entry.
Shorthand for plain values
If a child needs no validators, pass raw values. The builder wraps each one in a FormControl.
const tags = this.fb.array(['angular', 'forms']);
// FormArray<FormControl<string>>Use the shorthand for simple lists. Once an entry needs validators or its own structure, create it explicitly with fb.control() or fb.group().
An array inside a form group
An array usually lives inside a FormGroup, next to ordinary controls. The following example puts a list of order lines under the items key.
import { NonNullableFormBuilder, Validators } from '@angular/forms';
export function buildOrderForm(fb: NonNullableFormBuilder) {
const createItem = (name = '', quantity = 1) =>
fb.group({
name: [name, Validators.required],
quantity: [quantity, [Validators.required, Validators.min(1)]],
});
return fb.group({
customer: ['', Validators.required],
items: fb.array([createItem()], Validators.minLength(1)),
});
}The createItem function is the important part. A form array needs a fresh control each time you add a row, and you also need one when you populate the form from data. Defining it once avoids repeating the field definitions.
flowchart TDCommon mistakes
- Sharing one control instance. Pushing the same
FormControlorFormGroupobject into the array twice makes two rows edit the same state. Create a new instance for every row. - Using the array shorthand with an options object. Raw values only produce plain controls. Use
fb.control(value, options)when you needvalidatorsorupdateOn. - Expecting array validators to run per entry. A validator passed as the second argument to
fb.array()receives the whole array. Put per-entry rules on the children. - Injecting
FormBuilderand then expecting non-nullable types. PlainFormBuilderproducesstring | nullfor controls unless you passnonNullable: trueeach time.NonNullableFormBuildersets this for you.
Exercise
Write the fb.array() call, using an injected NonNullableFormBuilder named fb, for a list of phone numbers where each entry is required and the list must contain at least one entry. What is the resulting type?
Show answer
const phones = fb.array([fb.control('', Validators.required)], Validators.minLength(1)); The type is FormArray<FormControl<string>>. The required rule applies to each entry, and minLength(1) applies to the array.