Reactive Forms In Angular

This article covers all important features of Reactive Forms in Angular from basic to advanced scenarios.
 
Below is the list of topics that are covered in this article,
  • Introduction to forms in Angular and their differences
  • A basic example of Reactive Forms
  • Inbuilt and Custom Validations
  • Async validations
  • Conditionally managing validations
  • Dynamically adding form fields
So, let’s see each and every topic one by one.
 

Introduction

 
Angular is providing two ways to work with forms: template-driven forms and reactive forms. These both ways work differently.
 
The below information will help you to decide which type of form works best for your situation.
  • Reactive forms are more robust: they're more scalable, reusable, and testable. If forms are a key part of your application, use Reactive forms.
  • Template-driven forms are useful for adding a simple form to an app, such as an email list, signup form. They're easy to add to an app, but they don't scale as much as reactive forms. If you have very basic form requirements and logic, use template-driven forms.
(Source courtesy: https://angular.io)
 

Key differences

 
The table below summarizes the key differences between reactive and template-driven forms.
 
REACTIVE
TEMPLATE-DRIVEN
Setup (form model)
More explicit, created in the component class
Less explicit, created by directives
Data model
Structured
Unstructured
Predictability
Synchronous
Asynchronous
Form validation
Functions
Directives
Mutability
Immutable
Mutable
Scalability
Low-level API access
Abstraction on top of APIs
(Source courtesy: https://angular.io)
 

A basic example of Reactive Forms

 
What are Reactive forms?
 
At first sight, Reactive forms might seem complicated but they can be very useful when you actually get it. They are really powerful and flexible.
 
With Reactive forms, we don’t depend on design (template) for creating our forms but instead, here we use the API’s provided by angular which is called the Reactive API. These APIs can be used by importing a module called ReactiveModule to create the forms.
 
With Reactive Forms, all the declaration and structuring of the forms is to be done in the code (TS file). This model is then used in the template (HTML).
 
Reactive forms provide a model-driven approach to handling form inputs whose values change over time.”
 
Here’s how to get started with Reactive Forms, 
  1. Import Reactive forms module
  2. Define the form(formGroup) in a component class.
  3. Connect this form to your HTML form.
Import Reactive forms module
 
Firstly, we have to import ReactiveFormsModule instead of FormsModule in our app.module.ts or in your desired module.
  1. import { ReactiveFormsModule } from '@angular/forms';  
Define the form (formGroup) in a component class
 
In our component class, we will define the formGroup and also formControls within our formGroup.
  • formGroup
    Entire form will be treated as formGroup and here we can give a name to the formGroup.

  • formControl
    Each ‘input’ in our form will be mapped with formControl. This formControl is like our input box, select box, radio buttons etc. This form control will contain the value and validations of the form field.
In short, what we have to do is, we will first create formGroup’s object and then inside this formGroup we will create different formControl’s objects as per our need (each formControl object for each input, we require from user).In this way, our formGroup will contain a group (collection) of form controls. And by doing this our entire form is ready to be use in our template (HTML).
 
Now, one thing to be noted is that we want this form to be there when we load the page so we have to write this formGroup on our TS file’s ngOnInit() method and before starting we have to give a name or declare the formGroup.
 
So, here we are with the code.
 
signupForm: FormGroup; //declaring our form variable
  1. ngOnInit() {  
  2.     this.signupForm = new FormGroup({  
  3.       user_name: new FormControl(null),  
  4.       user_email: new FormControl(null),  
  5.       password_group: new FormGroup({  
  6.             user_password: new FormControl(null),  
  7.             user_confirmPassword: new FormControl(null),  
  8.             }),  
  9.       user_phone: new FormControl(null),  
  10.  user_gender: new FormControl('Male'),  
  11.       user_city: new FormControl('Ahmedabad'),  
  12.       user_notification: new FormControl('email')  
  13.     });  
  14.   }  
In the above code, we have initialized formGroup’s object with different form fields(formControl’s objects). And these formControls are having one argument that is ‘null’ for initializing our form fields. So initially all the form fields would be blank(null value). And if we want to assign some initial value like Gender and City in our case then we can give those values too.
 
In above code notice the ‘password_group’ field of the form. This is a formGroup inside our main formGroup(nesting of FormGroups). We have taken password_group because we want to consider password and confirm password fields as one group. So this way we can combine these formControls and make them one formGroup. 
 

Connect this form to your HTML form

 
Now, our Form is ready to be bind with our HTML. So moving to HTML file, we can bind the form like below:
  1. <form [formGroup]="signupForm" (ngSubmit)="onSubmit()"></form>  
formGroup - Binds the Form to the FormGroup we created in component i.e. with the signupform.
 
ngSubmit - This event will be triggered when we submit the form.
 
formControlName - Each form fields (inputs) should have formControlName, which is exactly the same as given in TS file. 
  1. <input type="text" placeholder="Name" formControlName="user_name">  
So, in short our entire HTML can look like this,
 
Note
In this example I have used Bootstrap for design purpose so you will find HTML according this.
  1. <div class="container">  
  2.   <form [formGroup]="signupForm" (ngSubmit)="onSubmit()">  
  3.   
  4.         <h3 style="text-align:center">  
  5.             Sign Up  
  6.         </h3>  
  7.   
  8.         <div class="form-group">  
  9.               <!-- Username -->  
  10.               <label for="username">Name: <span class="lblError">*</span></label>  
  11.               <div class="controls">  
  12.                 <input type="text" id="username" placeholder="Name" class="form-control" formControlName="user_name">  
  13.               </div>  
  14.         </div>  
  15.   
  16.         <div class="form-group">  
  17.           <div class="form-check form-check-inline">  
  18.             <!-- Notification -->  
  19.             <input type="radio" id="emailNotification" class="form-check-input" formControlName="user_notification" value="email">  
  20.             <label class="form-check-label" for="emailNotification">Email</label>  
  21.           </div>  
  22.           <div class="form-check form-check-inline">  
  23.             <input type="radio" id="phoneNotification" class="form-check-input" formControlName="user_notification" value="phone">  
  24.             <label class="form-check-label" for="phoneNotification">Phone</label>  
  25.           </div>  
  26.         </div>  
  27.   
  28.         <div class="form-row">  
  29.           <div class="col">  
  30.             <div class="form-group">  
  31.               <!-- E-mail -->  
  32.               <label for="email">E-mail: <span class="lblError">*</span></label>  
  33.               <div class="controls">  
  34.                 <input type="text" id="email" placeholder="E-mail" class="form-control" formControlName="user_email">  
  35.               </div>  
  36.             </div>  
  37.           </div>  
  38.           <div class="col">  
  39.             <div class="form-group">  
  40.               <!-- Mobile -->  
  41.               <label for="phone">Phone <span class="lblError">*</span></label>  
  42.               <div class="controls">  
  43.                 <input type="number" id="phone" placeholder="Phone" class="form-control" formControlName="user_phone">  
  44.               </div>  
  45.             </div>  
  46.           </div>  
  47.         </div>  
  48.   
  49.         <div class="form-row" formGroupName="password_group">  
  50.             <div class="col">  
  51.               <div class="form-group">  
  52.                 <div class="control-group">  
  53.                   <!-- Password -->  
  54.                   <label for="password">Password: <span class="lblError">*</span></label>  
  55.                   <div class="controls">  
  56.                   <input type="password" id="password" placeholder="Password" class="form-control" formControlName="user_password">  
  57.                   </div>  
  58.                 </div>  
  59.               </div>  
  60.             </div>  
  61.             <div class="col">  
  62.               <div class="form-group">  
  63.                 <div class="control-group">  
  64.                 <!-- Password (Confirm)-->  
  65.                 <label for="password_confirm">Password (Confirm): <span class="lblError">*</span></label>  
  66.                 <div class="controls">  
  67.                   <input type="password" id="password_confirm" placeholder="Password (Confirm)" class="form-control" formControlName="user_confirmPassword">  
  68.                 </div>  
  69.                 </div>  
  70.               </div>  
  71.             </div>  
  72.       </div>  
  73.   
  74.       <div class="form-group" >  
  75.         <div class="form-check form-check-inline">  
  76.             <!-- Gender -->  
  77.             <input type="radio" id="genderMale" class="form-check-input" formControlName="user_gender" value="Male">  
  78.             <label class="form-check-label" for="genderMale">Male</label>  
  79.         </div>  
  80.         <div class="form-check form-check-inline">  
  81.             <input type="radio" id="genderFemale" class="form-check-input" formControlName="user_gender" value="Female">  
  82.             <label class="form-check-label" for="genderFeMale">Female</label>  
  83.         </div>  
  84.       </div>  
  85.   
  86.         <div class="form-group">  
  87.             <!-- City -->  
  88.             <label for="city">City: <span class="lblError">*</span></label>  
  89.             <div class="controls">  
  90.                 <select id="city" formControlName="user_city" class="form-control">  
  91.                     <option value="Ahmedabad">Ahmedabad</option>  
  92.                     <option value="Baroda">Baroda</option>  
  93.                     <option value="Surat">Surat</option>  
  94.                 </select>  
  95.             </div>  
  96.         </div>  
  97.   
  98.         <div class="form-group">  
  99.           <button class="btn btn-primary" type="submit" style="width: 15%">  
  100.             Sign Up  
  101.           </button>  
  102.         </div>  
  103.   
  104.   </form>  
  105. </div>  
And lastly, the onSubmit() method in our TS file will just submit the form and inside this method we will just print the values entered using the form.
  1. onSubmit() {  
  2.     console.log(this.signupForm);  
  3.     console.log(this.signupForm.get('user_name').value);  
  4. }  
By doing all these, we are able to see the form as below.
 
Reactive Forms In Angular
 
And now, we will fill the form and click on the “Sign Up” button. The form will get submitted and onSubmit()method will be called so this will print the consoles in your browser as below,
 
Reactive Forms In Angular
Note
  1. console.log(this.signupForm); ==> This line will print the entire form’s object in the console. Here, you can see that entire form’s Status is Valid and Values is displaying all the data you have entered in the Form.
  2. console.log(this.signupForm.get('user_name').value); ==> This line will print individual fields of Form like UserName in this example.

Inbuilt and Custom Validations

 
Now it’s time to move on to the next topic and that is Validation in Reactive Forms.
 
Validation is the most basic requirement when you are working with the forms. Here, we will see how to validate the user input and will display validation messages accordingly.
 

Validator functions

 
There are two types of validator functions: sync validators and async validator.
  • Sync validators - these functions will require form control’s instance and immediately returns either validation errors or null.
  • Async validators - these functions will require form control’s instance and returns a Promise or Observable that later emits validation errors or null.
(Source courtesy: https://angular.io)
 
In this topic, we will focus only on Sync validations. For validators to work, we have to import Validators class on our TS file like below.
  1. import { FormGroup, FormControl, Validators } from '@angular/forms';  
And now let’s see how to use validations:
  1. ngOnInit() {  
  2. this.signupForm = new FormGroup({  
  3.       user_name: new FormControl(null, Validators.required),  
  4.       user_email: new FormControl(null, [Validators.email, Validators.required]),  
  5.   
  6.       password_group: new FormGroup({  
  7.             user_password: new FormControl(null, [Validators.required]),  
  8.             user_confirmPassword: new FormControl(null, [Validators.required]),  
  9.         }),  
  10.   
  11.       user_phone: new FormControl(null),  
  12.       user_gender: new FormControl('Male'),  
  13.       user_city: new FormControl('Ahmedabad', [Validators.required]),  
  14.       user_notification: new FormControl('email')  
  15. });  
  16. }  
As you can see Form Control’s instance will have second parameter as validation (single value or an array). This second parameter only accepts sync validations and custom validations. And if we want to assign Async validations then we have to give it as a third parameter to Form Control.
 
By now we have applied validations to our form’s inputs but here we also need to show the validation message to the end user. So for that in HTML file we will fetch (check) the validation errors and according to that will display the message.
  1. <p class="lblError" *ngIf="signupForm.get('user_name').hasError('required')  
  2.                             && !signupForm.get('user_name').pristine">  
  3.                     Name is Required  
  4. </p>  
Above <p> tag is showing validation message and this <p> tag should be visible to end user if and only if the validation error occurs. So for that we have to apply ‘IF’ condition on <p> tag. Here notice that we can reach to a control with either signupForm.get(‘user_name’) or with signupForm.controls.user_name and then we can reach to errors with either .hasError(‘required’) or with .errors?.required
 
Here, in IF condition, we have also included .prisitine method. This indicates the state of the input field. Angular provides various states as below mentioned:
  • valid: This property returns true if the element’s contents are valid and false otherwise.
  • invalid: This property returns true if the element’s contents are invalid and false otherwise.
  • pristine: This property returns true if the element’s contents have not been changed (controls just loaded then returns true).
  • dirty: This property returns true if the element’s contents have been changed.
  • untouched: This property returns true if the user has not visited the element.
  • touched: This property returns true if the user has visited the element.
So based on our requirement we can use any of above. In our example, we have used .pristine because we do not want to show the validation message when user has not changed the control’s value or we can say when controls are just loaded on the screen.
 
So, our entire form with validations can look like this.
  1. <div class="container">  
  2.   <form [formGroup]="signupForm" (ngSubmit)="onSubmit()">  
  3.   
  4.         <h3 style="text-align:center">  
  5.             Sign Up  
  6.         </h3>  
  7.   
  8.         <div class="form-group">  
  9.               <!-- Username -->  
  10.               <label for="username">Name: <span class="lblError">*</span></label>  
  11.               <div class="controls">  
  12.                 <input type="text" id="username" placeholder="Name" class="form-control" formControlName="user_name">  
  13.                 <p class="lblError" *ngIf="signupForm.get('user_name').hasError('required')  
  14.                             && !signupForm.get('user_name').pristine">  
  15.                     Name is Required  
  16.                 </p>  
  17.               </div>  
  18.         </div>  
  19.   
  20.         <div class="form-group">  
  21.           <div class="form-check form-check-inline">  
  22.             <!-- Notification -->  
  23.             <input type="radio" id="emailNotification" class="form-check-input" formControlName="user_notification" value="email">  
  24.             <label class="form-check-label" for="emailNotification">Email</label>  
  25.           </div>  
  26.           <div class="form-check form-check-inline">  
  27.             <input type="radio" id="phoneNotification" class="form-check-input" formControlName="user_notification" value="phone">  
  28.             <label class="form-check-label" for="phoneNotification">Phone</label>  
  29.           </div>  
  30.         </div>  
  31.   
  32.         <div class="form-row">  
  33.           <div class="col">  
  34.             <div class="form-group">  
  35.               <!-- E-mail -->  
  36.               <label for="email">E-mail: <span class="lblError">*</span></label>  
  37.               <div class="controls">  
  38.                 <input type="text" id="email" placeholder="E-mail" class="form-control" formControlName="user_email">  
  39.                 <p class="lblError" *ngIf="signupForm.get('user_email').hasError('required') && !signupForm.get('user_email').pristine">  
  40.                   Please provide your E-mail  
  41.                 </p>  
  42.                 <p class="lblError" *ngIf="signupForm.get('user_email').hasError('email') && !signupForm.get('user_email').pristine">E-mail is Invalid</p>  
  43.               </div>  
  44.             </div>  
  45.           </div>  
  46.           <div class="col">  
  47.             <div class="form-group">  
  48.               <!-- Mobile -->  
  49.               <label for="phone">Phone <span class="lblError">*</span></label>  
  50.               <div class="controls">  
  51.                 <input type="number" id="phone" placeholder="Phone" class="form-control" formControlName="user_phone">  
  52.                 <p class="lblError" *ngIf="signupForm.get('user_phone').hasError('required') && !signupForm.get('user_phone').pristine">  
  53.                     Phone is Required  
  54.                 </p>  
  55.               </div>  
  56.             </div>  
  57.           </div>  
  58.         </div>  
  59.   
  60.         <div class="form-row" formGroupName="password_group">  
  61.             <div class="col">  
  62.               <div class="form-group">  
  63.                 <div class="control-group">  
  64.                   <!-- Password -->  
  65.                   <label for="password">Password: <span class="lblError">*</span></label>  
  66.                   <div class="controls">  
  67.                   <input type="password" id="password" placeholder="Password" class="form-control" formControlName="user_password">  
  68.                   <p class="lblError" *ngIf="signupForm.get('password_group').get('user_password').hasError('required') && !signupForm.get('password_group').get('user_password').pristine">  
  69.                       Password is Required  
  70.                   </p>  
  71.                   </div>  
  72.                 </div>  
  73.               </div>  
  74.             </div>  
  75.             <div class="col">  
  76.               <div class="form-group">  
  77.                 <div class="control-group">  
  78.                 <!-- Password (Confirm)-->  
  79.                 <label for="password_confirm">Password (Confirm): <span class="lblError">*</span></label>  
  80.                 <div class="controls">  
  81.                   <input type="password" id="password_confirm" placeholder="Password (Confirm)" class="form-control" formControlName="user_confirmPassword">  
  82.                 </div>  
  83.                 </div>  
  84.               </div>  
  85.             </div>  
  86.       </div>  
  87.   
  88.       <div class="form-group" >  
  89.         <div class="form-check form-check-inline">  
  90.             <!-- Gender -->  
  91.             <input type="radio" id="genderMale" class="form-check-input" formControlName="user_gender" value="Male">  
  92.             <label class="form-check-label" for="genderMale">Male</label>  
  93.         </div>  
  94.         <div class="form-check form-check-inline">  
  95.             <input type="radio" id="genderFemale" class="form-check-input" formControlName="user_gender" value="Female">  
  96.             <label class="form-check-label" for="genderFeMale">Female</label>  
  97.         </div>  
  98.       </div>  
  99.   
  100.         <div class="form-group">  
  101.             <!-- City -->  
  102.             <label for="city">City: <span class="lblError">*</span></label>  
  103.             <div class="controls">  
  104.                 <select id="city" formControlName="user_city" class="form-control">  
  105.                     <option value="Ahmedabad">Ahmedabad</option>  
  106.                     <option value="Baroda">Baroda</option>  
  107.                     <option value="Surat">Surat</option>  
  108.                 </select>  
  109.             </div>  
  110.         </div>  
  111.   
  112.         <div class="form-group">  
  113.           <button class="btn btn-primary" type="submit" [disabled]="signupForm.invalid"  style="width: 15%">  
  114.             Sign Up  
  115.           </button>  
  116.         </div>  
  117.   
  118.   </form>  
  119. </div>  
Also, notice how we disabled the Submit button if the entire form in invalid.
 
Time to run the application..!!
 
Reactive Forms In Angular 
By default, our Submit button is disabled as we have not entered any values to the inputs but if we enter any value and then remove it and make the control blank again then it will display the validation message as shown above.
 

Custom Validations

 
If we need some specific validations then we can create our own custom validators also. These custom validators are just functions that we can create in our component file.
 
Consider a scenario where we want to create a custom validator that will take the user name and we will check that user name should not fall within the invalid name’s array declared by us. If so, then it will give error otherwise not.
 
In TS file create a dummy array like below:
  1. invalidNamesArr: string[] = ['Hello''Angular'];  
So, when a user tries to enter a user name like ‘Hello’ or ‘Angular’ it will indicate the error.
 
Now, we will create a custom validator function as below,
  1. invalidNameValidation(control: AbstractControl): {[key: string]: boolean} {  
  2.   if (this.invalidNamesArr.indexOf(control.value) >= 0) {  
  3.     return {invalidName: true};  
  4.   }  
  5.   return null;  
  6. }  
Now, we just need to call this validation function with username FormControl inside ngOnInit() method,
 
user_name: new FormControl(null, [Validators.required, this.invalidNameValidation.bind(this)])
 
Ok, so our validation has been applied so we can move to HTML file now and show the error message like below,
  1. <p class="lblError" *ngIf="signupForm.get('user_name').hasError('invalidName') && !signupForm.get('user_name').pristine">  
  2.     Name is Invalid  
  3. </p>  
Let’s check it out!
 
Reactive Forms In Angular
 

Async Validations

 
Let’s create a simple Async validation that checks against all the Email-ids of existing users and does not allow to enter the same Email-id to the new user.
 
It is always a best practice to keep the logic centralized as much as you can and put the reusable code in a separate file. So here we are creating a Utility folder and inside this folder, we are creating a normal TS file named as duplicateEmailCheck.ts.
 
In this file, we are having checkEmail method that will make the HTTP Get call using the object of UserService and that will return all the records of user from the database. Now, in this result, we will check that Email-Id entered by the new user matches with the existing data or not. If matched, then we will indicate duplicateEmail validation error.
 
Let us look at the actual code as below: (duplicateEmailCheck.ts)
  1. import { UserService } from "../signup/user.service";  
  2. import { AsyncValidatorFn, AbstractControl } from "@angular/forms";  
  3. import { Observable, of } from "rxjs";  
  4. import { map } from "rxjs/operators";  
  5. import { UserModel } from "../signup/userModel";  
  6.   
  7. export class DuplicateEmailCheck {  
  8.   static checkEmail(_serviceObj: UserService): AsyncValidatorFn {  
  9.     return (c: AbstractControl): Observable<{ [key: string]: boolean } | null> => {  
  10.   
  11.       if (c.value != null && c.value != '') {  
  12.         return _serviceObj.getAllUsers().pipe(  
  13.   
  14.           map((res: UserModel[]) => {  
  15.             if (res.length != 0) {  
  16.   
  17.               let matched: boolean = false;  
  18.               for (let index = 0; index < res.length; index++) {  
  19.                 if (res[index].user_email == c.value) {  
  20.                   matched = true;  
  21.                   break;  
  22.                 }  
  23.               }  
  24.   
  25.               if (matched) {  
  26.                 return { duplicateEmail: true };  
  27.               } else {  
  28.                 return null;  
  29.               }  
  30.   
  31.             } else {  
  32.               return null;  
  33.             }  
  34.   
  35.           })  
  36.         );  
  37.       }  
  38.   
  39.       return of(null);  
  40.     };  
  41.   }  
  42. }  
The above checkEmail method is accepting instance of a UserService as its argument and also we are having the value of input control on which we have applied this validator.
Now, it is time to bind this validation to the form control UserEmail! So let’s move to signup.component.ts file. Here, inside ngOnInit() method user_email FormControl will have third argument as a async validator.
  1. user_email: new FormControl(null, [Validators.email, Validators.required],DuplicateEmailCheck.checkEmail(this._userServiceObj))  
And finally we will move to HTML (signup.component.html) file and show the validation message.
  1. <p class="lblError" style="color:blue" *ngIf="(signupForm.get('user_email').status == 'PENDING' && !signupForm.get('user_email').pristine)">Checking...</p>  
  2. <p class="lblError" style="color:green" *ngIf="(signupForm.get('user_email').status == 'VALID'  && !signupForm.get('user_email').pristine && (!signupForm.get('user_email').value == '' ))">Email is Available</p>  
  3. <p class="lblError" style="color:red" *ngIf="signupForm.get('user_email').hasError('duplicateEmail')">Email is already Taken</p>  
You can see that we are displaying different messages depending on the value of the status property of the email form control. And the possible values for status are Valid, Invalid, Pending and Disabled. And main thing is error message for duplicateEmail, this message is for async validation applied over here.
 
Reactive Forms In Angular
 
Reactive Forms In Angular 

Conditionally managing validations

 
Have you noticed the two radio buttons Email and Phone on our HTML? Yes, these radio buttons are there to ask the user to select their choice to get the notification from Admin. Here, if the user selects Email then Email’s input box will have required validation and if the user selects Phone then Phone’s input box will have required validation. In short, we are conditionally managing the validation and we will display the error message based on which radio button has been selected by the user.
 
First of all, we will check that radio buttons’ values are changed by the user or not and if yes then we will invoke our validation method that will apply (bind) the validation accordingly.
  1. this.signupForm.get('user_notification').valueChanges.subscribe(  
  2.        x => this.setNotificationValidation(x));  
Above line of code should be placed inside ngOnInit() method but after the completion of FomGroup’s object’s initialization. Notice that here setNotificationValidation() is our customized validation method that will apply the validation.
  1. setNotificationValidation(value: string) {  
  2.     const phoneControl = this.signupForm.get('user_phone');  
  3.     const emailControl = this.signupForm.get('user_email');  
  4.     if (value == 'phone')  
  5.     {  
  6.       phoneControl.setValidators(Validators.required);  
  7.       emailControl.clearValidators();  
  8.       emailControl.setValidators(Validators.email);  
  9.     }  
  10.     else  
  11.     {  
  12.       phoneControl.clearValidators();          emailControl.setValidators([Validators.email,Validators.required]);  
  13.       emailControl.setAsyncValidators(DuplicateEmailCheck.checkEmail(this._userServiceObj));  
  14.     }  
  15.     phoneControl.updateValueAndValidity();  
  16.     emailControl.updateValueAndValidity();  
  17.   }  
Above method will first check the value of radio button is ‘Phone’ or not. If it is ‘Phone’ then we will apply (set) the validation for ‘Phone’ and remove (clear) the validation of ‘Email’ and vice versa. Notice the last two lines of above method. This updateValueAndValidity() method is inbuilt and this method will actually modify the form control’s validations and reflect the changes.
 
Ok. So, let me show you how it will actually look.
 
Reactive Forms In Angular
Reactive Forms In Angular

Dynamically adding form fields

 
We are having a form and now we would like to add form fields dynamically to form. So this is much easier with Reactive forms and FormArray. FormArray is much like a FormGroup but the difference is that it is an array of FormControls or FormGroups.
 
For this, we are having an example where we will ask the user to fill their ‘Hobbies’ and will allow the user to add their new ‘Hobbies’ dynamically one by one.
 
First in order to use this, we have to import FormArray to our component (signup.component.ts).
  1. import { FormGroup, FormControl, FormArray } from '@angular/forms';  
And now inside ngOnInit() we will simply have FormArray with its name:
  1. user_hobbies: new FormArray([])  
And then, in the HTML (signup.component.html), we will add following.
  1. <div class="form-group">  
  2.           <!-- Hobbies -->  
  3.           <label for="hobbies">Hobbies</label>  
  4.           <div class="controls">  
  5.             <button id="hobbies" type="button" class="btn btn-secondary" (click)="onAddHobbiesClick()" style="width: 8%"> Add</button>  
  6.             <div formArrayName="user_hobbies" *ngFor="let item of getControls();let i = index">  
  7.               <input type="text" [formControlName]="i" class="form-control">  
  8.               <button type="button" class="btn btn-danger btn-sm pull" (click)="onRemoveHobbiesClick(i)">Remove</button>  
  9.             </div>  
  10.           </div>  
  11.         </div>  
At first, we will have ‘Add’ button. By clicking this button it will create input box for Hobby run time. So, user can add as much Hobbies as they want. And with each input box of Hobby we are also providing ‘Remove’ button. So if user wants to remove the Hobby then they can do so by clicking ‘Remove’ button.
 
Also, notice getControls() method inside *ngFor. This method is having all the array elements or controls generated in user_hobbies array. So we are just iterating through all the controls and displaying them.
 
For ‘Add’ and ‘Remove’ buttons we have to specify their methods accordingly.
 
So, below is the code for signup.component.ts,
  1. getControls() {  
  2.     return (<FormArray>this.signupForm.get('user_hobbies')).controls;  
  3.  }  
  4.   
  5.   onAddHobbiesClick() {  
  6.     if (this.signupForm.get('user_hobbies').value.length < 3) {  
  7.       const control = new FormControl(null);  
  8.       (this.signupForm.get('user_hobbies') as FormArray).push(control);  
  9.     } else {  
  10.       alert('You can add maximum 3 Hobbies');  
  11.     }  
  12.   }  
  13.   
  14.   onRemoveHobbiesClick(i) {  
  15.     (this.signupForm.get('user_hobbies') as FormArray).removeAt(i);  
  16.   }  
Now as you can see above, interesting part is that we can treat FormArray just like a normal array and push and remove items from it.
 
Also, notice how we have restricted the user to add only 3 Hobbies. After adding 3 Hobbies if the user tries to add more, then it will show an alert message to him.
 
Reactive Forms In Angular
 
Reactive Forms In Angular
 
Reactive Forms In Angular
 
Thanks for reading this article. Hope it helps. You can find the full example on Github here,
  1. CREATE TABLE `user_tbl` (`user_id` int(11) NOT NULL,`user_name` varchar(100) NOT NULL,`user_email` varchar(150) DEFAULT NULL,`user_password` varchar(100) NOT NULL,`user_phone` int(11) DEFAULT NULL,`user_gender` varchar(50) DEFAULT NULL,`user_hobbies` varchar(500) DEFAULT NULL,`user_city` varchar(50) DEFAULT NULL)  


Similar Articles