Reactive forms provide a model-driven approach to handling form inputs whose values change over time. This guide shows you how to create and update a basic form control, progress to using multiple controls in a group, validate form values, and create dynamic forms where you can add or remove controls at run time.
Overview of reactive forms
Reactive forms use an explicit and immutable approach to managing the state of a form at a given point in time. Each change to the form state returns a new state, which maintains the integrity of the model between changes. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.
Reactive forms also provide a straightforward path to testing because you are assured that your data is consistent and predictable when requested. Any consumers of the streams have access to manipulate that data safely.
Reactive forms differ from template-driven forms in distinct ways. Reactive forms provide synchronous access to the data model, immutability with observable operators, and change tracking through observable streams.
Template-driven forms let direct access modify data in your template, but are less explicit than reactive forms because they rely on directives embedded in the template, along with mutable data to track changes asynchronously. See the Forms Overview for detailed comparisons between the two paradigms.
Adding a basic form control
There are three steps to using form controls.
- Generate a new component and register the reactive forms module. This module declares the reactive-form directives that you need to use reactive forms.
- Instantiate a new
FormControl. - Register the
FormControlin the template.
You can then display the form by adding the component to the template.
The following examples show how to add a single form control. In the example, the user enters their name into an input field, captures that input value, and displays the current value of the form control element.
-
Generate a new component and import the ReactiveFormsModule
Use the CLI command
ng generate componentto generate a component in your project and importReactiveFormsModulefrom the@angular/formspackage and add it to your Component'simportsarray.src/app/name-editor/name-editor.component.ts (excerpt)
import {Component} from '@angular/core';import {FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-name-editor', templateUrl: './name-editor.component.html', styleUrls: ['./name-editor.component.css'], imports: [ReactiveFormsModule],})export class NameEditorComponent { name = new FormControl(''); updateName() { this.name.setValue('Nancy'); }} -
Declare a FormControl instance
Use the constructor of
FormControlto set its initial value, which in this case is an empty string. By creating these controls in your component class, you get immediate access to listen for, update, and validate the state of the form input.src/app/name-editor/name-editor.component.ts
import {Component} from '@angular/core';import {FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-name-editor', templateUrl: './name-editor.component.html', styleUrls: ['./name-editor.component.css'], imports: [ReactiveFormsModule],})export class NameEditorComponent { name = new FormControl(''); updateName() { this.name.setValue('Nancy'); }} -
Register the control in the template
After you create the control in the component class, you must associate it with a form control element in the template. Update the template with the form control using the
formControlbinding provided byFormControlDirective, which is also included in theReactiveFormsModule.src/app/name-editor/name-editor.component.html
<label for="name">Name: </label><input id="name" type="text" [formControl]="name"><p>Value: {{ name.value }}</p><button type="button" (click)="updateName()">Update Name</button>Using the template binding syntax, the form control is now registered to the
nameinput element in the template. The form control and DOM element communicate with each other: the view reflects changes in the model, and the model reflects changes in the view. -
Display the component
The
FormControlassigned to thenameproperty is displayed when the<app-name-editor>component is added to a template.src/app/app.component.html (name editor)
<h1>Reactive Forms</h1><app-name-editor /><app-profile-editor />
Displaying a form control value
You can display the value in the following ways.
- Through the
valueChangesobservable where you can listen for changes in the form's value in the template usingAsyncPipeor in the component class using thesubscribe()method - With the
valueproperty, which gives you a snapshot of the current value
The following example shows you how to display the current value using interpolation in the template.
src/app/name-editor/name-editor.component.html (control value)
<label for="name">Name: </label><input id="name" type="text" [formControl]="name"><p>Value: {{ name.value }}</p><button type="button" (click)="updateName()">Update Name</button>
The displayed value changes as you update the form control element.
Reactive forms provide access to information about a given control through properties and methods provided with each instance. These properties and methods of the underlying AbstractControl class are used to control form state and determine when to display messages when handling input validation.
Read about other FormControl properties and methods in the API Reference.
Replacing a form control value
Reactive forms have methods to change a control's value programmatically, which gives you the flexibility to update the value without user interaction.
A form control instance provides a setValue() method that updates the value of the form control and validates the structure of the value provided against the control's structure.
For example, when retrieving form data from a backend API or service, use the setValue() method to update the control to its new value, replacing the old value entirely.
The following example adds a method to the component class to update the value of the control to Nancy using the setValue() method.
src/app/name-editor/name-editor.component.ts (update value)
import {Component} from '@angular/core';import {FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-name-editor', templateUrl: './name-editor.component.html', styleUrls: ['./name-editor.component.css'], imports: [ReactiveFormsModule],})export class NameEditorComponent { name = new FormControl(''); updateName() { this.name.setValue('Nancy'); }}
Update the template with a button to simulate a name update. When you click the Update Name button, the value entered in the form control element is reflected as its current value.
src/app/name-editor/name-editor.component.html (update value)
<label for="name">Name: </label><input id="name" type="text" [formControl]="name"><p>Value: {{ name.value }}</p><button type="button" (click)="updateName()">Update Name</button>
The form model is the source of truth for the control, so when you click the button, the value of the input is changed within the component class, overriding its current value.
HELPFUL: In this example, you're using a single control.
When using the setValue() method with a form group or form array instance, the value needs to match the structure of the group or array.
Grouping form controls
Forms typically contain several related controls. Reactive forms provide two ways of grouping multiple related controls into a single input form.
| Form groups | Details |
|---|---|
| Form group | Defines a form with a fixed set of controls that you can manage together. Form group basics are discussed in this section. You can also nest form groups to create more complex forms. |
| Form array | Defines a dynamic form, where you can add and remove controls at run time. You can also nest form arrays to create more complex forms. For more about this option, see Creating dynamic forms. |
Just as a form control instance gives you control over a single input field, a form group instance tracks the form state of a group of form control instances (for example, a form). Each control in a form group instance is tracked by name when creating the form group. The following example shows how to manage multiple form control instances in a single group.
Generate a ProfileEditor component and import the FormGroup and FormControl classes from the @angular/forms package.
ng generate component ProfileEditor
src/app/profile-editor/profile-editor.component.ts (imports)
import {Component} from '@angular/core';import {FormGroup, FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { profileForm = new FormGroup({ firstName: new FormControl(''), lastName: new FormControl(''), address: new FormGroup({ street: new FormControl(''), city: new FormControl(''), state: new FormControl(''), zip: new FormControl(''), }), }); updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); }}
To add a form group to this component, take the following steps.
-
Create a FormGroup instance
Create a property in the component class named
profileFormand set the property to a new form group instance. To initialize the form group, provide the constructor with an object of named keys mapped to their control.For the profile form, add two form control instances with the names
firstNameandlastNamesrc/app/profile-editor/profile-editor.component.ts (form group)
import {Component} from '@angular/core';import {FormGroup, FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { profileForm = new FormGroup({ firstName: new FormControl(''), lastName: new FormControl(''), address: new FormGroup({ street: new FormControl(''), city: new FormControl(''), state: new FormControl(''), zip: new FormControl(''), }), }); updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); }}The individual form controls are now collected within a group. A
FormGroupinstance provides its model value as an object reduced from the values of each control in the group. A form group instance has the same properties (such asvalueanduntouched) and methods (such assetValue()) as a form control instance. -
Associate the FormGroup model and view
A form group tracks the status and changes for each of its controls, so if one of the controls changes, the parent control also emits a new status or value change. The model for the group is maintained from its members. After you define the model, you must update the template to reflect the model in the view.
src/app/profile-editor/profile-editor.component.html (template form group)
<form [formGroup]="profileForm"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for(alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias: </label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div></form><p>Form Value: {{ profileForm.value | json }}</p><button type="button" (click)="updateProfile()">Update Profile</button>Just as a form group contains a group of controls, the profileForm
FormGroupis bound to theformelement with theFormGroupdirective, creating a communication layer between the model and the form containing the inputs. TheformControlNameinput provided by theFormControlNamedirective binds each individual input to the form control defined inFormGroup. The form controls communicate with their respective elements. They also communicate changes to the form group instance, which provides the source of truth for the model value. -
Save form data
The
ProfileEditorcomponent accepts input from the user, but in a real scenario you want to capture the form value and make it available for further processing outside the component. TheFormGroupdirective listens for thesubmitevent emitted by theformelement and emits anngSubmitevent that you can bind to a callback function. Add anngSubmitevent listener to theformtag with theonSubmit()callback method.src/app/profile-editor/profile-editor.component.html (submit event)
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" required /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for (alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias:</label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div> <p>Complete the form to enable button.</p> <button type="submit" [disabled]="!profileForm.valid">Submit</button></form><hr><p>Form Value: {{ profileForm.value | json }}</p><p>Form Status: {{ profileForm.status }}</p><button type="button" (click)="updateProfile()">Update Profile</button>The
onSubmit()method in theProfileEditorcomponent captures the current value ofprofileForm. UseEventEmitterto keep the form encapsulated and to provide the form value outside the component. The following example usesconsole.warnto log a message to the browser console.src/app/profile-editor/profile-editor.component.ts (submit method)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }}The
submitevent is emitted by theformtag using the built-in DOM event. You trigger the event by clicking a button withsubmittype. This lets the user press the Enter key to submit the completed form.Use a
buttonelement to add a button to the bottom of the form to trigger the form submission.src/app/profile-editor/profile-editor.component.html (submit button)
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" required /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for (alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias:</label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div> <p>Complete the form to enable button.</p> <button type="submit" [disabled]="!profileForm.valid">Submit</button></form><hr><p>Form Value: {{ profileForm.value | json }}</p><p>Form Status: {{ profileForm.status }}</p><button type="button" (click)="updateProfile()">Update Profile</button>The button in the preceding snippet also has a
disabledbinding attached to it to disable the button whenprofileFormis invalid. You aren't performing any validation yet, so the button is always enabled. Basic form validation is covered in the Validating form input section. -
Display the component
To display the
ProfileEditorcomponent that contains the form, add it to a component template.src/app/app.component.html (profile editor)
<h1>Reactive Forms</h1><app-name-editor /><app-profile-editor />ProfileEditorlets you manage the form control instances for thefirstNameandlastNamecontrols within the form group instance.Creating nested form groups
Form groups can accept both individual form control instances and other form group instances as children. This makes composing complex form models easier to maintain and logically group together.
When building complex forms, managing the different areas of information is easier in smaller sections. Using a nested form group instance lets you break large forms groups into smaller, more manageable ones.
To make more complex forms, use the following steps.
- Create a nested group.
- Group the nested form in the template.
Some types of information naturally fall into the same group. A name and address are typical examples of such nested groups, and are used in the following examples.
To create a nested group in `profileForm`, add a nested `address` element to the form group instance. src/app/profile-editor/profile-editor.component.ts (nested form group)
import {Component} from '@angular/core';import {FormGroup, FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { profileForm = new FormGroup({ firstName: new FormControl(''), lastName: new FormControl(''), address: new FormGroup({ street: new FormControl(''), city: new FormControl(''), state: new FormControl(''), zip: new FormControl(''), }), }); updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); }}In this example,
address groupcombines the currentfirstNameandlastNamecontrols with the newstreet,city,state, andzipcontrols. Even though theaddresselement in the form group is a child of the overallprofileFormelement in the form group, the same rules apply with value and status changes. Changes in status and value from the nested form group propagate to the parent form group, maintaining consistency with the overall model. -
Group the nested form in the template
After you update the model in the component class, update the template to connect the form group instance and its input elements. Add the
addressform group containing thestreet,city,state, andzipfields to theProfileEditortemplate.src/app/profile-editor/profile-editor.component.html (template nested form group)
<form [formGroup]="profileForm"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for(alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias: </label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div></form><p>Form Value: {{ profileForm.value | json }}</p><button type="button" (click)="updateProfile()">Update Profile</button>The
ProfileEditorform is displayed as one group, but the model is broken down further to represent the logical grouping areas.Display the value for the form group instance in the component template using the
valueproperty andJsonPipe.
Updating parts of the data model
When updating the value for a form group instance that contains multiple controls, you might only want to update parts of the model. This section covers how to update specific parts of a form control data model.
There are two ways to update the model value:
| Methods | Details |
|---|---|
setValue() |
Set a new value for an individual control. The setValue() method strictly adheres to the structure of the form group and replaces the entire value for the control. |
patchValue() |
Replace any properties defined in the object that have changed in the form model. |
The strict checks of the setValue() method help catch nesting errors in complex forms, while patchValue() fails silently on those errors.
In ProfileEditorComponent, use the updateProfile method with the following example to update the first name and street address for the user.
src/app/profile-editor/profile-editor.component.ts (patch value)
import {Component} from '@angular/core';import {FormGroup, FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { profileForm = new FormGroup({ firstName: new FormControl(''), lastName: new FormControl(''), address: new FormGroup({ street: new FormControl(''), city: new FormControl(''), state: new FormControl(''), zip: new FormControl(''), }), }); updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); }}
Simulate an update by adding a button to the template to update the user profile on demand.
src/app/profile-editor/profile-editor.component.html (update value)
<form [formGroup]="profileForm"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for(alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias: </label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div></form><p>Form Value: {{ profileForm.value | json }}</p><button type="button" (click)="updateProfile()">Update Profile</button>
When a user clicks the button, the profileForm model is updated with new values for firstName and street. Notice that street is provided in an object inside the address property.
This is necessary because the patchValue() method applies the update against the model structure.
PatchValue() only updates properties that the form model defines.
Using the FormBuilder service to generate controls
Creating form control instances manually can become repetitive when dealing with multiple forms.
The FormBuilder service provides convenient methods for generating controls.
Use the following steps to take advantage of this service.
- Import the
FormBuilderclass. - Inject the
FormBuilderservice. - Generate the form contents.
The following examples show how to refactor the ProfileEditor component to use the form builder service to create form control and form group instances.
-
Import the FormBuilder class
Import the
FormBuilderclass from the@angular/formspackage.src/app/profile-editor/profile-editor.component.ts (import)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {FormArray} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: [''], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); }} -
Inject the FormBuilder service
The
FormBuilderservice is an injectable provider from the reactive forms module. Use theinject()function to inject this dependency in your component.src/app/profile-editor/profile-editor.component.ts (property init)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {FormArray} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: [''], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); }} -
Generate form controls
The
FormBuilderservice has three methods:control(),group(), andarray(). These are factory methods for generating instances in your component classes including form controls, form groups, and form arrays. Use thegroupmethod to create theprofileFormcontrols.src/app/profile-editor/profile-editor.component.ts (form builder)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {FormArray} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: [''], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); }}In the preceding example, you use the
group()method with the same object to define the properties in the model. The value for each control name is an array containing the initial value as the first item in the array.TIP: You can define the control with just the initial value, but if your controls need sync or async validation, add sync and async validators as the second and third items in the array. Compare using the form builder to creating the instances manually.
src/app/profile-editor/profile-editor.component.ts (instances)
import {Component} from '@angular/core';import {FormGroup, FormControl, ReactiveFormsModule} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { profileForm = new FormGroup({ firstName: new FormControl(''), lastName: new FormControl(''), address: new FormGroup({ street: new FormControl(''), city: new FormControl(''), state: new FormControl(''), zip: new FormControl(''), }), }); updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); }}src/app/profile-editor/profile-editor.component.ts (form builder)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {FormArray} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: [''], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); }}
Validating form input
Form validation is used to ensure that user input is complete and correct. This section covers adding a single validator to a form control and displaying the overall form status. Form validation is covered more extensively in the Form Validation guide.
Use the following steps to add form validation.
- Import a validator function in your form component.
- Add the validator to the field in the form.
- Add logic to handle the validation status.
The most common validation is making a field required.
The following example shows how to add a required validation to the firstName control and display the result of validation.
-
Import a validator function
Reactive forms include a set of validator functions for common use cases. These functions receive a control to validate against and return an error object or a null value based on the validation check.
Import the
Validatorsclass from the@angular/formspackage.src/app/profile-editor/profile-editor.component.ts (import)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }} -
Make a field required
In the
ProfileEditorcomponent, add theValidators.requiredstatic method as the second item in the array for thefirstNamecontrol.src/app/profile-editor/profile-editor.component.ts (required validator)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }} -
Display form status
When you add a required field to the form control, its initial status is invalid. This invalid status propagates to the parent form group element, making its status invalid. Access the current status of the form group instance through its
statusproperty.Display the current status of
profileFormusing interpolation.src/app/profile-editor/profile-editor.component.html (display status)
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" required /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for (alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias:</label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div> <p>Complete the form to enable button.</p> <button type="submit" [disabled]="!profileForm.valid">Submit</button></form><hr><p>Form Value: {{ profileForm.value | json }}</p><p>Form Status: {{ profileForm.status }}</p><button type="button" (click)="updateProfile()">Update Profile</button>The Submit button is disabled because
profileFormis invalid due to the requiredfirstNameform control. After you fill out thefirstNameinput, the form becomes valid and the Submit button is enabled.For more on form validation, visit the Form Validation guide.
Creating dynamic forms
FormArray is an alternative to FormGroup for managing any number of unnamed controls.
As with form group instances, you can dynamically insert and remove controls from form array instances, and the form array instance value and validation status is calculated from its child controls.
However, you don't need to define a key for each control by name, so this is a great option if you don't know the number of child values in advance.
To define a dynamic form, take the following steps.
- Import the
FormArrayclass. - Define a
FormArraycontrol. - Access the
FormArraycontrol with a getter method. - Display the form array in a template.
The following example shows you how to manage an array of aliases in ProfileEditor.
-
Import the
FormArrayclassImport the
FormArrayclass from@angular/formsto use for type information. TheFormBuilderservice is ready to create aFormArrayinstance.src/app/profile-editor/profile-editor.component.ts (import)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {FormArray} from '@angular/forms';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: [''], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); }} -
Define a
FormArraycontrolYou can initialize a form array with any number of controls, from zero to many, by defining them in an array. Add an
aliasesproperty to the form group instance forprofileFormto define the form array.Use the
FormBuilder.array()method to define the array, and theFormBuilder.control()method to populate the array with an initial control.src/app/profile-editor/profile-editor.component.ts (aliases form array)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }}The aliases control in the form group instance is now populated with a single control until more controls are added dynamically.
-
Access the
FormArraycontrolA getter provides access to the aliases in the form array instance compared to repeating the
profileForm.get()method to get each instance. The form array instance represents an undefined number of controls in an array. It's convenient to access a control through a getter, and this approach is straightforward to repeat for additional controls.Use the getter syntax to create an
aliasesclass property to retrieve the alias's form array control from the parent form group.src/app/profile-editor/profile-editor.component.ts (aliases getter)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }}Because the returned control is of the type
AbstractControl, you need to provide an explicit type to access the method syntax for the form array instance. Define a method to dynamically insert an alias control into the alias's form array. TheFormArray.push()method inserts the control as a new item in the array, and you can also pass an array of controls to FormArray.push() to register multiple controls at once.src/app/profile-editor/profile-editor.component.ts (add alias)
import {Component, inject} from '@angular/core';import {FormBuilder, ReactiveFormsModule} from '@angular/forms';import {Validators} from '@angular/forms';import {FormArray} from '@angular/forms';import {JsonPipe} from '@angular/common';@Component({ selector: 'app-profile-editor', templateUrl: './profile-editor.component.html', styleUrls: ['./profile-editor.component.css'], imports: [ReactiveFormsModule, JsonPipe],})export class ProfileEditorComponent { private formBuilder = inject(FormBuilder); profileForm = this.formBuilder.group({ firstName: ['', Validators.required], lastName: [''], address: this.formBuilder.group({ street: [''], city: [''], state: [''], zip: [''], }), aliases: this.formBuilder.array([this.formBuilder.control('')]), }); get aliases() { return this.profileForm.get('aliases') as FormArray; } updateProfile() { this.profileForm.patchValue({ firstName: 'Nancy', address: { street: '123 Drew Street', }, }); } addAlias() { this.aliases.push(this.formBuilder.control('')); } onSubmit() { // TODO: Use EventEmitter with form value console.warn(this.profileForm.value); }}In the template, each control is displayed as a separate input field.
-
Display the form array in the template
To attach the aliases from your form model, you must add it to the template. Similar to the
formGroupNameinput provided byFormGroupNameDirective,formArrayNamebinds communication from the form array instance to the template withFormArrayNameDirective.Add the following template HTML after the
<div>closing theformGroupNameelement.src/app/profile-editor/profile-editor.component.html (aliases form array template)
<form [formGroup]="profileForm" (ngSubmit)="onSubmit()"> <label for="first-name">First Name: </label> <input id="first-name" type="text" formControlName="firstName" required /> <label for="last-name">Last Name: </label> <input id="last-name" type="text" formControlName="lastName" /> <div formGroupName="address"> <h2>Address</h2> <label for="street">Street: </label> <input id="street" type="text" formControlName="street" /> <label for="city">City: </label> <input id="city" type="text" formControlName="city" /> <label for="state">State: </label> <input id="state" type="text" formControlName="state" /> <label for="zip">Zip Code: </label> <input id="zip" type="text" formControlName="zip" /> </div> <div formArrayName="aliases"> <h2>Aliases</h2> <button type="button" (click)="addAlias()">+ Add another alias</button> @for (alias of aliases.controls; track $index; let i = $index) { <div> <!-- The repeated alias template --> <label for="alias-{{ i }}">Alias:</label> <input id="alias-{{ i }}" type="text" [formControlName]="i" /> </div> } </div> <p>Complete the form to enable button.</p> <button type="submit" [disabled]="!profileForm.valid">Submit</button></form><hr><p>Form Value: {{ profileForm.value | json }}</p><p>Form Status: {{ profileForm.status }}</p><button type="button" (click)="updateProfile()">Update Profile</button>The
@forblock iterates over each form control instance provided by the aliases form array instance. Because form array elements are unnamed, you assign the index to theivariable and pass it to each control to bind it to theformControlNameinput.Each time a new alias instance is added, the new form array instance is provided its control based on the index. This lets you track each individual control when calculating the status and value of the root control.
-
Add an alias
Initially, the form contains one
Aliasfield. To add another field, click the Add Alias button. You can also validate the array of aliases reported by the form model displayed byForm Valueat the bottom of the template. Instead of a form control instance for each alias, you can compose another form group instance with additional fields. The process of defining a control for each item is the same.
Unified control state change events
All form controls expose a single unified stream of control state change events through the events observable on AbstractControl (FormControl, FormGroup, FormArray, and FormRecord).
This unified stream lets you react to value, status, pristine, touched and reset state changes and also for form-level actions such as submit , allowing you to handle all updates with a one subscription instead of wiring multiple observables.
Event types
Each item emitted by events is an instance of a specific event class:
ValueChangeEvent— when the control’s value changes.StatusChangeEvent— when the control’s validation status updates to one of theFormControlStatusvalues (VALID,INVALID,PENDING, orDISABLED).PristineChangeEvent— when the control’s pristine/dirty state changes.TouchedChangeEvent— when the control’s touched/untouched state changes.FormResetEvent— when a control or form is reset, either via thereset()API or a native action.FormSubmittedEvent— when the form is submitted.
All event classes extend ControlEvent and include a source reference to the AbstractControl that originated the change, which is useful in large forms.
import { Component } from '@angular/core';import { FormControl, ValueChangeEvent, StatusChangeEvent, PristineChangeEvent, TouchedChangeEvent, FormResetEvent, FormSubmittedEvent, ReactiveFormsModule, FormGroup,} from '@angular/forms';@Component({/* ... */ })export class UnifiedEventsBasicComponent { form = new FormGroup({ username: new FormControl(''), }); constructor() { this.form.events.subscribe((e) => { if (e instanceof ValueChangeEvent) { console.log('Value changed to: ', e.value); } if (e instanceof StatusChangeEvent) { console.log('Status changed to: ', e.status); } if (e instanceof PristineChangeEvent) { console.log('Pristine status changed to: ', e.pristine); } if (e instanceof TouchedChangeEvent) { console.log('Touched status changed to: ', e.touched); } if (e instanceof FormResetEvent) { console.log('Form was reset'); } if (e instanceof FormSubmittedEvent) { console.log('Form was submitted'); } }); }}
Filtering specific events
Prefer RxJS operators when you only need a subset of event types.
import { filter } from 'rxjs/operators';import { StatusChangeEvent } from '@angular/forms';control.events .pipe(filter((e) => e instanceof StatusChangeEvent)) .subscribe((e) => console.log('Status:', e.status));
Unifying from multiple subscriptions
Before
import { combineLatest } from 'rxjs/operators';combineLatest([control.valueChanges, control.statusChanges]) .subscribe(([value, status]) => { /* ... */ });
After
control.events.subscribe((e) => { // Handle ValueChangeEvent, StatusChangeEvent, etc.});
NOTE: On value change, the emit happens right after a value of this control is updated. The value of a parent control (for example if this FormControl is a part of a FormGroup) is updated later, so accessing a value of a parent control (using the value property) from the callback of this event might result in getting a value that has not been updated yet. Subscribe to the events of the parent control instead.
Utility functions for narrowing form control types
Angular provides four utility functions that help determine the concrete type of an AbstractControl. These functions act as type guards and narrow the control type when they return true, which lets you safely access subtype-specific properties inside the same block.
| Utility function | Details |
|---|---|
isFormControl |
Returns true when the control is a FormControl. |
isFormGroup |
Returns true when the control is a FormGroup |
isFormRecord |
Returns true when the control is a FormRecord |
isFormArray |
Returns true when the control is a FormArray |
These helpers are particularly useful in custom validators, where the function signature receives an AbstractControl, but the logic is intended for a specific control kind.
import { AbstractControl, isFormArray } from '@angular/forms';export function positiveValues(control: AbstractControl) { if (!isFormArray(control)) { return null; // Not a FormArray: validator is not applicable. } // Safe to access FormArray-specific API after narrowing. const hasNegative = control.controls.some(c => c.value < 0); return hasNegative ? { positiveValues: true } : null;}
Reactive forms API summary
The following table lists the base classes and services used to create and manage reactive form controls. For complete syntax details, see the API reference documentation for the Forms package.
Classes
| Class | Details |
|---|---|
AbstractControl |
The abstract base class for the concrete form control classes FormControl, FormGroup, and FormArray. It provides their common behaviors and properties. |
FormControl |
Manages the value and validity status of an individual form control. It corresponds to an HTML form control such as <input> or <select>. |
FormGroup |
Manages the value and validity state of a group of AbstractControl instances. The group's properties include its child controls. The top-level form in your component is FormGroup. |
FormArray |
Manages the value and validity state of a numerically indexed array of AbstractControl instances. |
FormBuilder |
An injectable service that provides factory methods for creating control instances. |
FormRecord |
Tracks the value and validity state of a collection of FormControl instances, each of which has the same value type. |
Directives
| Directive | Details |
|---|---|
FormControlDirective |
Syncs a standalone FormControl instance to a form control element. |
FormControlName |
Syncs FormControl in an existing FormGroup instance to a form control element by name. |
FormGroupDirective |
Syncs an existing FormGroup instance to a DOM element. |
FormGroupName |
Syncs a nested FormGroup instance to a DOM element. |
FormArrayName |
Syncs a nested FormArray instance to a DOM element. |