Reactive Forms In Angular
One of the two ways to design Angular forms is with reactive forms (also known as model-driven forms). In this article, we will learn how to create a simple Example of Reactive Form. To create reactive forms, we must first import the ReactiveFormsModule. We then use Form Group, Form Control, and Form Arrays to generate the Form Model in the component class. The HTML form template will then be created and bound to the Form Model.
What is Reactive Forms?
Reactive forms are those in which the form's structure is defined in the component class. Form Group, Form Control, and Form Arrays are used to build the form model. The validation rules are also defined in the component class. Then, in the template, we bind it to the HTML form. This differs from template-driven forms, in which the logic and controls are defined in the HTML template.
How to use Reactive Forms
- ReactiveFormsModule should be imported.
- Using Form Group, Form Control, and Form Arrays, create a Form Model in a component class.
- Make an HTML form that looks like the Form Model.
- Create a link between the HTML form and the Form Model.
Reactive Forms Example Application
To create a new application, use ng new command.
ng new reactive-forms --routing=true --style=css
Import ReactiveFormsModule
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { ReactiveFormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
Model
import { FormGroup, FormControl, Validators } from '@angular/forms'
FormGroup
contactForm = new FormGroup({})
FormControl
contactForm = new FormGroup({ firstname: new FormControl(), lastname: new FormControl(), email: new FormControl(), gender: new FormControl(), isMarried: new FormControl(), country: new FormControl() })
HTML Form
<form"> <p> <label for="firstname">First Name </label> <input type="text" id="firstname" name="firstname"> </p> <p> <label for="lastname">Last Name </label> <input type="text" id="lastname" name="lastname"> </p> <p> <label for="email">Email </label> <input type="text" id="email" name="email"> </p> <p> <label for="gender">Geneder </label> <input type="radio" value="male" id="gender" name="gender"> Male <input type="radio" value="female" id="gender" name="gender"> Female </p> <p> <label for="isMarried">Married </label> <input type="checkbox" id="isMarried" name="isMarried"> </p> <p> <label for="country">country </label> <select id="country" name="country"> <option [ngValue]="c.id" *ngFor="let c of countryList"> {{c.name}} </option> </select> </p> <p> <button type="submit">Submit</button> </p> </form>
Binding the template to the model
<form [formGroup]="contactForm">
<input type="text" id="firstname" name="firstname" formControlName="firstname"> <input type="text" id="lastname" name="lastname" formControlName="lastname">
Submit form
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()">
<form [formGroup]="contactForm" (ngSubmit)="onSubmit()"> <p> <label for="firstname">First Name </label> <input type="text" id="firstname" name="firstname" formControlName="firstname"> </p> <p> <label for="lastname">Last Name </label> <input type="text" id="lastname" name="lastname" formControlName="lastname"> </p> <p> <label for="email">Email </label> <input type="text" id="email" name="email" formControlName="email"> </p> <p> <label for="gender">Geneder </label> <input type="radio" value="male" id="gender" name="gender" formControlName="gender"> Male <input type="radio" value="female" id="gender" name="gender" formControlName="gender"> Female </p> <p> <label for="isMarried">Married </label> <input type="checkbox" id="isMarried" name="isMarried" formControlName="isMarried"> </p> <p> <label for="country">country </label> <select id="country" name="country" formControlName="country"> <option [ngValue]="c.id" *ngFor="let c of countryList"> {{c.name}} </option> </select> </p> <p> <button type="submit">Submit</button> </p> </form>
Receive the data in the Component class
onSubmit() { console.log(this.contactForm.value); }
Test the form
FormControl
Default Value
//Setting Default value as string firstname= new FormControl(‘Sachin’); //Setting Default value & disabled state as object firstname: new FormControl({value: ‘Rahul’, disabled: true}),
Sync Validator
firstname: new FormControl('', [Validators.required,Validators.minLength(10)]),
Asynchronous validator
Grouping the controls using FormGroup
contactForm = new FormGroup({ firstname: new FormControl(), lastname: new FormControl(), email: new FormControl(), gender: new FormControl(), isMarried: new FormControl(), country: new FormControl(), address:new FormGroup({ city: new FormControl(), street: new FormControl(), pincode:new FormControl() }) })
<div formGroupName="address"> <div class="form-group"> <label for="city">City</label> <input type="text" class="form-control" name="city" formControlName="city" > </div> <div class="form-group"> <label for="street">Street</label> <input type="text" class="form-control" name="street" formControlName="street" > </div> <div class="form-group"> <label for="pincode">Pin Code</label> <input type="text" class="form-control" name="pincode" formControlName="pincode"> </div> </div>