Sub topic 9
Less Boilerplate with FormBuilder
This chapter assumes you can already build a FormGroup from new FormControl(...) calls and that you know how strictly typed, nonNullable controls behave. It also uses inject() inside a standalone component.
The problem FormBuilder solves
Writing new FormControl('', { nonNullable: true, validators: [...] }) for every field gets repetitive. A form with ten fields contains ten constructor calls, and each one needs the nonNullable option. FormBuilder is an injectable service with factory methods (group, control, array, record). These methods produce the same FormGroup, FormControl, and FormArray instances from a shorter configuration.
Using NonNullableFormBuilder
Inject NonNullableFormBuilder when you want every control to reset to its initial value instead of to null. You can also call inject(FormBuilder).nonNullable, which returns the same thing.
import { Component, inject } from '@angular/core';
import { NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
@Component({
selector: 'app-profile-form',
standalone: true,
imports: [ReactiveFormsModule],
template: `
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
<input formControlName="name" placeholder="Name" />
<input type="email" formControlName="email" placeholder="Email" />
<label><input type="checkbox" formControlName="newsletter" /> Newsletter</label>
<button type="submit" [disabled]="profileForm.invalid">Save</button>
</form>
`,
})
export class ProfileFormComponent {
private fb = inject(NonNullableFormBuilder);
profileForm = this.fb.group({
name: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
newsletter: false,
});
onSubmit(): void {
console.log(this.profileForm.getRawValue());
}
}TypeScript infers profileForm as FormGroup<{ name: FormControl<string>; email: FormControl<string>; newsletter: FormControl<boolean> }>. You get full typing without writing the type yourself.
Configuration shapes
Each entry passed to group() can take one of several shapes:
| Entry | Result |
|---|---|
| `false` | Control with initial value `false`, no validators |
| `['', Validators.required]` | Value plus one sync validator |
| `['', [v1, v2]]` | Value plus several sync validators |
| `['', syncValidators, asyncValidators]` | Value, sync validators, async validators |
| `this.fb.control('', { updateOn: 'blur' })` | Full options object, for settings the array form cannot express |
| `this.fb.group({...})` | Nested group |
The common mistake: plain FormBuilder
If you inject FormBuilder and do not use .nonNullable, each control is typed FormControl<string | null>, and calling reset() sets the values to null. Your submit handler then has to deal with null values it never expected. Pick NonNullableFormBuilder by default. When a single field really can be empty, model it explicitly, for example with this.fb.control<string | null>(null).
Exercise
You inject plain FormBuilder and write fb.group({ city: 'Paris' }). What is the type of the city control, and what does city hold after form.reset()? How would you change the code so reset() restores 'Paris'?
Show answer
The control is typed FormControl<string | null>, and after reset() its value is null. Use inject(NonNullableFormBuilder) or inject(FormBuilder).nonNullable instead. The control is then FormControl<string>, and reset() restores 'Paris'.
Exercise
Write a fb.group entry for a username field that starts empty, is required, and runs an async validator usernameAvailable(checker).
Show answer
username: ['', Validators.required, usernameAvailable(checker)]. The third array element holds the async validator or validators.