liveBook · Manning

FormGroup

As explained in Pro Angular 16

FormGroup is an Angular reactive-forms class that aggregates named form controls and exposes their combined value and status.

FormGroup is an Angular reactive-forms class that collects named FormControl objects into one manageable form model. It supports collective value, status, validation, and control operations while preserving access to individual controls. The ngForm directive wraps a FormGroup for template-oriented forms, whereas reactive forms use the class directly in a component. section 21.2

Group product controls

Create a group by mapping control names to existing controls: `productForm = new FormGroup({ name: this.nameField, category: this.categoryField });`. The names become the keys used for reading, writing, resetting, and locating controls. In the template, bind the group with `[formGroup]="productForm"`, then connect inputs with `formControlName="name"` and `formControlName="category"`.

Use the group API to manage the form as a unit:

  • Subscribe to `statusChanges` to detect group validation status; the group is invalid when any individual control is invalid. section 21.4

Validation can be queried through the group with `getError(validator, path)` and `hasError(validator, path)`, where the path identifies a named control such as `category`. For repeated messages, retrieve the control with `get` and let a directive subscribe to its status changes. section 21.4.3

How Pro Angular 16 covers it

How deep it goes
Explains construction, template binding, control management, values, reset behavior, aggregate status, and validation-error lookup.
Examples
Uses a product form with name and category controls, edit/reset behavior, status subscriptions, and validation-message directives.
What it assumes
Assumes familiarity with Angular forms and FormControl, while establishing FormGroup as their container and reactive-forms building block.
What you can do after
Build and operate grouped reactive forms with collective updates, resets, validation status checks, and named-control access.
How it connects
Connects FormControl, ngForm, formGroup, formControlName, validators, statusChanges, and ReactiveFormsModule.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.