Angular Reactive Forms
Learn how to create and manage Angular forms programmatically using FormControl, FormGroup, FormBuilder and Validators.
What are Reactive Forms?
Reactive Forms provide a programmatic approach to creating and managing Angular forms.
The form model is created in the component using objects such as FormControl and FormGroup.
Reactive forms are especially useful when an application needs complex validation, dynamic controls or precise control over form state.
Reactive Form Flow
Main Reactive Form Concepts
FormControl
Represents a single form field and its state.
FormGroup
Groups multiple controls into a form model.
FormBuilder
Provides a convenient way to create form controls and groups.
Validators
Apply validation rules to form controls.
Import ReactiveFormsModule
Reactive forms use Angular's ReactiveFormsModule.
In a standalone component, the module can be included in the component's imports array.
import { Component } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-registration',
imports: [ReactiveFormsModule],
templateUrl: './registration.component.html'
})
export class RegistrationComponent {
}
Creating FormControl
FormControl represents one individual form field.
It stores the field value and tracks states such as valid, invalid, touched and dirty.
name = new FormControl('');
name = new FormControl('CIIT Training Institute');
Creating FormGroup
FormGroup combines multiple FormControls into a single form model.
It allows related fields to be managed together.
registrationForm = new FormGroup({
name: new FormControl(''),
email: new FormControl(''),
mobile: new FormControl(''),
course: new FormControl('')
});
Connecting FormGroup with Template
The formGroup directive connects the HTML form with the FormGroup created in TypeScript.
Individual controls are connected using formControlName.
<form [formGroup]="registrationForm">
<input
type="text"
formControlName="name">
<input
type="email"
formControlName="email">
<input
type="text"
formControlName="mobile">
<select formControlName="course">
<option value="">
Select Course
</option>
<option value="Java Full Stack">
Java Full Stack
</option>
<option value=".NET Full Stack">
.NET Full Stack
</option>
</select>
</form>
Using FormBuilder
FormBuilder provides helper methods that make creating reactive form controls and groups more concise.
It is useful when a form contains multiple controls.
import { Component } from '@angular/core';
import {
FormBuilder,
Validators
} from '@angular/forms';
@Component({
selector: 'app-enquiry',
templateUrl: './enquiry.component.html'
})
export class EnquiryComponent {
enquiryForm = this.fb.group({
name: [''],
email: [''],
mobile: [''],
course: ['']
});
constructor(
private fb: FormBuilder
) {
}
}
Using Validators
Angular provides built-in validators that can be attached to reactive form controls.
Examples include required, email, minLength and maxLength.
registrationForm = new FormGroup({
name: new FormControl(
'',
Validators.required
),
email: new FormControl(
'',
[
Validators.required,
Validators.email
]
)
});
Multiple Validators
mobile = new FormControl(
'',
[
Validators.required,
Validators.minLength(10),
Validators.maxLength(10)
]
);
Mobile Validation
The mobile field is required and must contain the expected number of characters.
CIIT Training Institute Example
Suppose CIIT Training Institute has a registration form for visitors interested in a technical course.
Reactive Forms can define the complete form structure and validation rules inside the component.
import { Component } from '@angular/core';
import {
FormBuilder,
Validators
} from '@angular/forms';
@Component({
selector: 'app-course-registration',
templateUrl: './course-registration.component.html'
})
export class CourseRegistrationComponent {
registrationForm = this.fb.group({
name: [
'',
Validators.required
],
email: [
'',
[
Validators.required,
Validators.email
]
],
mobile: [
'',
[
Validators.required,
Validators.minLength(10),
Validators.maxLength(10)
]
],
course: [
'',
Validators.required
]
});
constructor(
private fb: FormBuilder
) {
}
submitForm() {
if (this.registrationForm.valid) {
console.log(
this.registrationForm.value
);
}
}
}
<form
[formGroup]="registrationForm"
(ngSubmit)="submitForm()">
<label>
Name
</label>
<input
type="text"
formControlName="name">
<label>
Email
</label>
<input
type="email"
formControlName="email">
<label>
Mobile
</label>
<input
type="text"
formControlName="mobile">
<label>
Course
</label>
<select
formControlName="course">
<option value="">
Select Course
</option>
<option value="Java Full Stack">
Java Full Stack
</option>
<option value=".NET Full Stack">
.NET Full Stack
</option>
<option value="Python Full Stack">
Python Full Stack
</option>
</select>
<button
type="submit"
[disabled]="registrationForm.invalid">
Register
</button>
</form>
CIIT Training Institute Registration
Name: Required
Email: Valid Email Required
Mobile: 10 Characters Required
Course: Select Course
Reading Form Values
Reactive form values can be accessed through the value property of the FormGroup.
This is useful when form data needs to be processed or sent to another service.
console.log(
this.registrationForm.value
);
console.log(
this.registrationForm.get('email')?.value
);
Checking Form Status
if (this.registrationForm.valid) {
console.log('Form is valid');
}
if (this.registrationForm.invalid) {
console.log('Form is invalid');
}
Displaying Validation Messages
Validation messages can be displayed by checking the control's validation state.
A common pattern is to display a message after the user has interacted with the control.
<input
type="email"
formControlName="email">
<div
*ngIf="
registrationForm.get('email')?.invalid &&
registrationForm.get('email')?.touched
">
Please enter a valid email address.
</div>
Accessing a FormControl
get emailControl() {
return this.registrationForm.get('email');
}
<div
*ngIf="emailControl?.invalid &&
emailControl?.touched">
Please enter a valid email.
</div>
Resetting a Reactive Form
The reset() method can be used to reset the form values and state.
This is useful after a successful submission or when the user wants to clear the form.
this.registrationForm.reset();
Enable and Disable Controls
this.registrationForm
.get('email')
?.disable();
this.registrationForm
.get('email')
?.enable();
Dynamic Validation
Reactive forms make it possible to change validators programmatically when application requirements change.
This is one of the reasons reactive forms are useful for complex form scenarios.
const mobileControl =
this.registrationForm.get('mobile');
mobileControl?.setValidators([
Validators.required,
Validators.minLength(10)
]);
mobileControl?.updateValueAndValidity();
Reactive Form Features
Explicit Model
Form structure is defined in TypeScript.
Validation
Validators can be defined programmatically.
Dynamic Forms
Controls and validators can be changed dynamically.
State Tracking
Form and control states can be inspected directly.
Reactive Forms vs Template-driven Forms
| Feature | Reactive Forms | Template-driven Forms |
|---|---|---|
| Form Model | Explicitly defined in TypeScript | Primarily represented in the template |
| FormControl | Directly used | Managed through directives |
| Validation | Programmatic | Template-oriented |
| Dynamic Controls | Convenient for complex scenarios | Less convenient for complex scenarios |
| Typical Use | Complex and dynamic forms | Simpler forms |
Important Points
Reactive forms define the form structure explicitly inside the component.
Use FormControl for individual fields and FormGroup for a collection of controls.
Validation rules can be attached directly to reactive form controls.
Reactive forms are suitable when a form requires advanced validation or dynamic behavior.
Reactive Forms Best Practices
- Keep the form model clearly defined in the component.
- Use meaningful names for controls and groups.
- Add appropriate validators to important fields.
- Check form validity before processing submitted data.
- Keep complex form-related logic in TypeScript instead of placing unnecessary logic in the template.
- Use reactive forms when dynamic controls or complex validation are required.
Summary
Reactive Forms provide a programmatic way to create, validate and manage Angular forms.
Important building blocks include FormControl, FormGroup, FormBuilder and Validators.
Reactive forms are especially useful for forms that require explicit form models, advanced validation or dynamic behavior. In the next topic, we will learn about Form Validation.