Sub topic 3
Grouping Controls with FormGroup
This chapter assumes you know TypeScript generics, have created a standalone FormControl, and have imported ReactiveFormsModule. A single FormControl tracks one value. Real forms have several fields that belong together, such as a name and an email on a profile. A FormGroup collects named controls into one object, so you can read, validate, and submit them as a unit.
The working model
A FormGroup is a container. Its keys are field names and its values are child controls. The group derives its own state from those children:
valueis an object with one property per enabled child.validis true only when every child is valid and the group's own validators pass.dirtyandtouchedare true when any child is dirty or touched.
Changes flow upward. When a child's value changes, Angular recalculates the parent's value and status straight away.
Creating a group
import { FormControl, FormGroup, Validators } from '@angular/forms';
const profileForm = new FormGroup({
name: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
email: new FormControl('', { nonNullable: true }),
});
console.log(profileForm.value); // { name: '', email: '' }
console.log(profileForm.valid); // false, because name is required
profileForm.controls.name.setValue('Ada');
console.log(profileForm.value); // { name: 'Ada', email: '' }
console.log(profileForm.valid); // trueThis code runs without a template. The form model is a plain object, so you can create and inspect it in a component class, a service, or a unit test.
Accessing child controls
There are two ways to reach a child control:
| Approach | Example | Type safety | Column |
|---|---|---|---|
| `controls` property | `profileForm.controls.email` | Fully typed. A typo is a compile error. | |
| `get()` method | `profileForm.get('email')` | Returns `AbstractControl \ | null`. A typo returns `null`. |
Use controls by default. Use get() with a dotted path such as 'address.city' when you need to reach into nested groups.
Common errors
- Reading
valueand expecting every field.valueleaves out disabled controls. A later chapter coversgetRawValue(), which includes them. - Using
get()with a misspelled key. You getnullwith no compile error, and the failure shows up later as a runtime error. Prefercontrols. - Replacing a child by reassignment. Writing
profileForm.controls.name = new FormControl()breaks the parent-child link. Change the value withsetValueorpatchValueinstead. - Expecting group validity to ignore children. One invalid child makes the whole group invalid, even if the group's own validators pass.
Exercise
A FormGroup has two controls: city (required, value 'Oslo') and zip (required, value ''). What are group.value and group.valid, and what is the type of group.get('zipp')?
Show answer
group.value is { city: 'Oslo', zip: '' } and group.valid is false, because zip fails Validators.required. group.get('zipp') is typed AbstractControl | null and returns null at runtime because of the typo. The compiler does not catch it.
Exercise
Why is profileForm.controls.email preferred over profileForm.get('email') in a typed form?
Show answer
controls.email is typed as FormControl<string>, so a misspelled key causes a compile error and value types carry through. get('email') returns a loosely typed AbstractControl | null, which needs null checks and hides typos until runtime.