Sub topic 2
FormControl and FormGroup in brief
This chapter assumes you know TypeScript, Angular components and templates, and standalone components that import ReactiveFormsModule. It reviews the two building blocks that FormArray is built from. If you already use FormControl and FormGroup comfortably, skim the "How state rolls up" section and move on.
FormControl: one value and its state
A FormControl tracks a single value and the state around it: whether it is valid, whether the user has touched it, and whether it has changed.
import { FormControl, Validators } from '@angular/forms';
const email = new FormControl('', {
nonNullable: true,
validators: [Validators.required, Validators.email],
});
console.log(email.valid); // false, because the value is empty
email.setValue('[email protected]');
console.log(email.valid); // true
email.reset();
console.log(email.value); // ''The nonNullable: true option makes reset() return the control to its initial value. Without it, reset() sets the value to null, and the control's type becomes string | null.
FormGroup: named controls in one object
A FormGroup holds controls under fixed names. Its value is an object with one property per control.
import { FormControl, FormGroup, Validators } from '@angular/forms';
const profile = new FormGroup({
name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
age: new FormControl(18, { nonNullable: true }),
});
profile.controls.name.setValue('Ada');
console.log(profile.value); // { name: 'Ada', age: 18 }
profile.controls.name.setValue('');
console.log(profile.valid); // falseThe keys are decided when you write the code. That is the limit FormArray removes: a group suits a form whose fields you know in advance, such as name and age. It does not suit a list whose length the user decides.
How state rolls up
A group is valid only when every child is valid. The same rule applies to dirty, touched, and pending. A group's status is computed from its children, and a change in a child updates the parent.
FormArray follows the same rolling-up rule. An invalid entry anywhere in the array makes the array invalid, and so the whole form.
Common errors
- Forgetting
nonNullable. Controls then havenullin their types, andreset()clears values instead of restoring them. - Reading
group.valueand expecting disabled fields.valueleaves out disabled controls.getRawValue()includes them. The same difference matters later when you submit an array. - Using
setValuewith a partial object.setValueon a group requires every key. UsepatchValuewhen you have only some of them. - Assuming the group validates itself. A group's validity comes only from its children, unless you attach a validator to the group.
Where this leads
A FormArray is a third kind of container. It holds controls by position instead of by name. Groups and controls will appear again as the entries inside arrays, for example one FormGroup per order line.
Exercise
A FormGroup has two controls, name (required) and age (no validators). name is empty and age holds 30. What are group.valid and group.value, and what would make valid true?
Show answer
group.valid is false, because one invalid child makes the whole group invalid. group.value is { name: '', age: 30 }. The group becomes valid once name receives a non-empty value, for example group.controls.name.setValue('Ada').