FormGroup class tracks the value and validity state of a group of FormControl instances. It aggregates the values of each child control into one object, with each control name as the key. It calculates its status by reducing the status values of its children.
FormGroup is one of the four fundamental building blocks used to define forms in Angular, along with FormControl, FormArray, and FormRecord.
Import
Constructor
A collection of child controls. The key for each child is the name under which it is registered.
A synchronous validator function, an array of such functions, or an
AbstractControlOptions object that contains validation functions and a validation trigger.A single async validator or array of async validator functions.
Basic Usage
Creating a FormGroup
Type Safety
FormGroup accepts a generic type argument that describes its inner controls:
Optional Controls
Properties
A collection of child controls keyed by name.
The aggregate value of the FormGroup, including only enabled controls. Disabled controls are excluded.
The validation status of the group. A FormGroup is invalid if any of its children are invalid.
A group is valid when its status is
VALID.A group is invalid when any of its child controls are invalid.
An object containing any errors generated by group-level validators.
A group is pristine if all of its children are pristine.
A group is dirty if any of its children are dirty.
A group is touched if any of its children are touched.
A multicasting observable that emits an event every time the value of the group changes.
A multicasting observable that emits an event every time the validation status recalculates.
Methods
get()
Retrieves a child control given the control’s name or path.The path to the control. Can be a string or an array for nested paths.
setValue()
Sets the value of the FormGroup. It accepts an object that matches the structure of the group.The new value for the group that matches the structure of the group.
When true, each change only affects this control, and not its parent.
When true, both the
statusChanges and valueChanges observables emit events.patchValue()
Patches the value of the FormGroup. It accepts an object with control names as keys and does its best to match the values to the correct controls.The object that matches the structure of the group.
When true, each change only affects this control, and not its parent.
When true, both the
statusChanges and valueChanges observables emit events.reset()
Resets the FormGroup, marking all descendantspristine and untouched, and resetting the value of all descendants to their default values or null.
Resets the group with an initial value. Can include form state objects with
value and disabled properties.When true, each change only affects this control, and not its parent.
When true, both the
statusChanges and valueChanges observables emit events.getRawValue()
The aggregate value of the FormGroup, including any disabled controls. Retrieves all values regardless of disabled status.addControl()
Adds a control to this group. Updates the value and validity of the control.The control name to add to the collection.
Provides the control for the given name.
When true, both the
statusChanges and valueChanges observables emit events.removeControl()
Removes a control from this group. Updates the value and validity of the control.The control name to remove from the collection.
When true, both the
statusChanges and valueChanges observables emit events.setControl()
Replaces an existing control. If a control with the given name does not exist, it will be added.The control name to replace in the collection.
Provides the control for the given name.
When true, both the
statusChanges and valueChanges observables emit events.contains()
Checks whether there is an enabled control with the given name in the group.The control name to check for existence in the collection.
registerControl()
Registers a control with the group’s list of controls. Does not update the value or validity of the control.Group-Level Validators
You can add validators that consider the value of multiple child controls:Update Strategies
Set a defaultupdateOn value for all child controls:
Examples
Nested Form Groups
Dynamic Form Controls
Listening to Value Changes
Conditional Validation
FormRecord
FormRecord is a variant of FormGroup for dynamic keys:
See Also
- FormControl - Track individual form control values
- FormArray - Manage an array of form controls
- Validators - Built-in validation functions
- Reactive Forms Guide
- Typed Forms Guide
