Ionic 4 – Reactive Forms

Angular, which is the default framework for starting Ionic applications provide 2 kinds of forms, reactive forms, and template-driven forms.

In this post, we will only discuss the implementation of Reactive Forms in an Ionic 4 app.

Reactive Forms

Reactive forms provide a model-driven approach to handling form inputs whose values change over time.

Reactive forms use an explicit and immutable approach to managing the state of a form at a given point in time. Each change to the form state returns a new state, which maintains the integrity of the model between changes. Reactive forms are built around observable streams, where form inputs and values are provided as streams of input values, which can be accessed synchronously.
Login screen validation using Reactive Forms

Let’s try to take an example of a login screen. We will be using Reactive Forms here.

Step 1 – app.module.ts

Importing the ReactiveFormsModule in app.module.ts

import { ReactiveFormsModule } from '@angular/forms';

Add the ReactiveFormsModule to the imports array of app.module.ts

   declarations: [AppComponent],
   entryComponents: [],
   imports: [

Step 2 – Create a module called login

In the login.module.ts, import the following:

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

Add the following modules to the imports array of login.module.ts

   imports: [

Step 3 – Use FormBuilder and FormControl

Listing of

import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
   selector: 'app-login',
   templateUrl: '',
   styleUrls: [''],
 export class LoginPage {
   public loginForm: FormGroup;
   constructor(private storage: Storage, 
               private formBuilder:FormBuilder){
     this.loginForm ={
       username: new FormControl('', Validators.compose([
       password: new FormControl('', Validators.compose([
 onClickSubmit() {
     console.log('Start login with: ' 
                 + this.loginForm.value.username + ':' 
                 + this.loginForm.value.password);

We create a loginForm(FormGroup) to hold FormControls like username and password. You can also add any number of validations that you need.

There’s also a function onClickSubmit to submit data when the validations are done.

Step 4 – Creating the form

Listing of

   <ion-toolbar color="primary">
       Ionic 4 App
 <ion-content padding="true">
   <ion-row class="ion-justify-content-center">
     <ion-col size-sm="12" size-lg="5">
       <div class="ion-padding login-card">
           <ion-card-header color="secondary" class="login-card-header">
             <form (ngSubmit)="onClickSubmit()" padding-right [formGroup]="loginForm">
               <ion-item lines="inset">
                 <ion-label position="floating">Username</ion-label>
                 <ion-input  formControlName="username"></ion-input>
               <ion-item lines="inset">
                 <ion-label position="floating">Password</ion-label>
                 <ion-input formControlName="password" type="password"></ion-input>
               <ion-item lines="none" class="ion-item-right-margin">
                 <ion-button size="default" slot="end" type="submit" [disabled]="!loginForm.valid">Submit</ion-button>

Username and password input fields have a named FormControlName attribute which is defined in the

How does it work?

Whenever the value in either text fields are changed, the validations are automatically applied, and loginForm.valid value changes. This field in turn, controls the Submit button.

The loginForm is synchronously updated as soon as any changes are detected on the fields inside the FormGroup.

2 thoughts on “Ionic 4 – Reactive Forms

  • May 23, 2020 at 5:37 am

    Hi. Good Night.
    Good post !!
    Helped me with reactive forms.
    A question:
    How can I do to work with ion-toggle ?
    Could you explain it?

    • May 24, 2020 at 3:35 am

      Let me know if you have any specific question about ion-toggle.


Leave a Reply

Your email address will not be published. Required fields are marked *