Nesting of Forms in Angular

In this article, we will learn the Nesting of Forms in Angular. FormGroup is used to give the complete form that will be bound to the HTML form with all the individual controls that are corresponding to their FormControl defined in the component class.

Introduction

 
In Angular, Reactive form application is used where most of the business logic related to validations and functionality are written on component class not in HTML pages. FormGroup is used to give the complete form that will bind to the HTML form with all the individual controls that are corresponding to their FormControl defined in the component class.
 
Sometimes we have complex scenarios where the forms may contain some subforms and so on. In such a scenario FormGroup class can also be used to group together some different form controls.
 
Let us suppose we have the address form that contains some fields like country, state, city, pin. Then we can have a separate formGroup for handling address that will have different properties inside it.
  

Prerequisites

  • HTML, CSS, and JS
  • Basics of angular

Implementation

 
Let us create a simple application that utilizes Reactive forms.  
  • Create a TestApp project using Angular CLI.
  • Create a test component which will contain the forms and related field:
  • Open test.component.ts and edit with below contents:
  1. import { Component, OnInit } from '@angular/core';   
  2. import { FormGroup, FormControl, FormControlName } from '@angular/forms';   
  3.   
  4. @Component({   
  5.   selector: 'app-test',   
  6.   templateUrl: './test.component.html',   
  7.   styleUrls: ['./test.component.css']   
  8. })   
  9.   
  10. exportclass TestComponent implements OnInit {   
  11.   registrationForm: any;   
  12.   graduationData: any;   
  13.   pgData: any;   
  14.   constructor() { }   
  15.   
  16.   ngOnInit() {   
  17.   this.graduationData = ['BCA', 'BBA', 'BE', 'B.Tech', 'B.Sc'];   
  18.   this.pgData = ['MCA', 'MBA', 'M.Tech', 'M.Sc'];   
  19.     this.registrationForm= new FormGroup({   
  20.      userName : new FormControl('Irshad'),   
  21.      password : new FormControl(''),   
  22.      email : new FormControl(''),   
  23.      phone : new FormControl(''),   
  24.      address : new FormGroup({   
  25.        country : new FormControl(''),   
  26.        state : new FormControl(''),   
  27.        city : new FormControl(''),   
  28.        pin : new FormControl('')   
  29.      }),   
  30.      qualification : new FormGroup({   
  31.        graduation : new FormControl(''),   
  32.        pg : new FormControl('')   
  33.      })   
  34.    });   
  35.   }   
  36. }  
We have added a group in address and qualification fields. So we have used FormGroup class for both and also added the corresponding objects.
 
Open test.component.html and edit with below contents:
  1. <div class="container-fluid">   
  2.     <h3>Registration Form</h3>   
  3.     <form [formGroup]="registrationForm">   
  4.         <div class="row">   
  5.             <div class="col-sm-6">   
  6.                 <div class="form-group">   
  7.                     <label>UserName</label>   
  8.                     <input formControlName="userName" type="text"class="form-control">   
  9.                 </div>   
  10.             </div>   
  11.             <div class="col-sm-6">   
  12.                 <div class="form-group">   
  13.                     <label>Password</label>   
  14.                     <input formControlName="password" type="Password"class="form-control">   
  15.                 </div>   
  16.             </div>   
  17.         </div>   
  18.         <div class="row">   
  19.             <div class="col-sm-6">   
  20.                 <div class="form-group">   
  21.                     <label>Email Address</label>   
  22.                     <input formControlName="email" type="email"class="form-control">   
  23.                 </div>   
  24.             </div>   
  25.             <div class="col-sm-6">   
  26.                 <div class="form-group">   
  27.                     <label>Phone</label>   
  28.                     <input formControlName="phone" type="Phone"class="form-control">   
  29.                 </div>   
  30.             </div>   
  31.         </div>   
  32.         <div formGroupName="address">   
  33.             <div class="row">   
  34.                 <div class="col-sm-6">   
  35.                     <div class="form-group">   
  36.                         <label>Country</label>   
  37.                         <input formControlName="country" type="text"class="form-control">   
  38.                     </div>   
  39.                 </div>   
  40.                 <div class="col-sm-6">   
  41.                     <div class="form-group">   
  42.                         <label>State</label>   
  43.                         <input formControlName="state" type="text"class="form-control">   
  44.                     </div>   
  45.                 </div>   
  46.             </div>   
  47.             <div class="row">   
  48.                 <div class="col-sm-6">   
  49.                     <div class="form-group">   
  50.                         <label>City</label>   
  51.                         <input formControlName="city" type="text"class="form-control">   
  52.                     </div>   
  53.                 </div>   
  54.                 <div class="col-sm-6">   
  55.                     <div class="form-group">   
  56.                         <label>Pin</label>   
  57.                         <input formControlName="pin" type="text"class="form-control">   
  58.                     </div>   
  59.                 </div>   
  60.             </div>   
  61.         </div>   
  62. <div formGroupName="qualification">   
  63.     <div class="row">   
  64.         <div class="col-sm-6">   
  65.             <div class="form-group">   
  66.                 <label>Graduation</label>   
  67.                 <select formControlName="graduation"class="form-control">   
  68.                     <option value="">Please select</option>   
  69.                     <option *ngFor="let value of graduationData">{{value}}</option>   
  70.                 </select>   
  71.             </div>   
  72.         </div>   
  73.         <div class="col-sm-6">   
  74.             <div class="form-group">   
  75.                 <label>PG</label>   
  76.                 <select formControlName="pg"class="form-control">   
  77.                     <option value="">Please select</option>   
  78.                     <option *ngFor="let value of pgData">{{value}}</option>  
  79.                 </select>   
  80.             </div>   
  81.         </div>   
  82.     </div>   
  83. </div>   
  84. <div class="row">   
  85.     <div class="col-sm-6">   
  86.         <button class="btn btn-primary" type="submit">Register</button>   
  87.     </div>   
  88. </div>   
  89. </form>   
  90. {{registrationForm.value | json}}   
  91. </div>  
We need to define the formGroupName directive to assign the address/qualification formGroup, so that the Angular treat the whole div tag as a formGroup and maps the form controls to within the address/qualification formGroupName to the formGroup.
 
Save and Run the application
image1
 
image2 
 
You can see the values that we have printed via interpolation in JSON pipe notation.
 
Thank you.