0
0
Angularframework~5 mins

ReactiveFormsModule setup in Angular

Choose your learning style9 modes available
Introduction

ReactiveFormsModule helps you build forms in Angular with clear, easy-to-manage code. It makes handling form inputs and validation simple and organized.

When you want to create a form with complex validation rules.
When you need to react to form input changes instantly.
When you want to keep form logic separate from the template.
When building forms that require dynamic controls or arrays.
When you want better control over form state and validation.
Syntax
Angular
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [
    ReactiveFormsModule
  ]
})
export class YourModule { }

You must import ReactiveFormsModule in the Angular module where you want to use reactive forms.

This setup allows you to use FormGroup, FormControl, and FormBuilder in your components.

Examples
Basic import of ReactiveFormsModule in the root module to enable reactive forms app-wide.
Angular
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  imports: [ReactiveFormsModule]
})
export class AppModule { }
Importing ReactiveFormsModule in a feature module alongside CommonModule.
Angular
import { ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

@NgModule({
  imports: [CommonModule, ReactiveFormsModule]
})
export class FeatureModule { }
Sample Program

This component shows a simple reactive form with two fields: first name and age. It uses ReactiveFormsModule features like FormGroup and FormControl. The form disables the submit button if the first name is empty and shows a message after submission.

Angular
import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-simple-form',
  template: `
    <form [formGroup]="profileForm" (ngSubmit)="onSubmit()">
      <label for="firstName">First Name:</label>
      <input id="firstName" formControlName="firstName" />
      <div *ngIf="firstName.invalid && firstName.touched" style="color:red">
        First Name is required.
      </div>

      <label for="age">Age:</label>
      <input id="age" type="number" formControlName="age" />

      <button type="submit" [disabled]="profileForm.invalid">Submit</button>
    </form>
    <p *ngIf="submitted">Form Submitted! Value: {{ profileForm.value | json }}</p>
  `
})
export class SimpleFormComponent {
  profileForm = new FormGroup({
    firstName: new FormControl('', Validators.required),
    age: new FormControl('')
  });

  submitted = false;

  get firstName() {
    return this.profileForm.get('firstName')!;
  }

  onSubmit() {
    this.submitted = true;
  }
}
OutputSuccess
Important Notes

Always import ReactiveFormsModule in the module where your reactive form components live.

Use formControlName in the template to link inputs to FormControl instances.

Validators help enforce rules like required fields and can be combined.

Summary

ReactiveFormsModule enables building forms with clear, reactive code.

Import it in your Angular module to start using reactive forms.

Use FormGroup and FormControl to manage form state and validation.