Dynamic Forms with Ionic Reactive Forms
Q: How do you use Ionic reactive forms and form builders to create more complex forms with dynamic fields and custom validation rules?
- Ionic
- Mid level question
Explore all the latest Ionic interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Ionic interview for FREE!
Ionic provides support for both template-driven and reactive forms. Reactive forms provide more control and flexibility for complex forms with dynamic fields and custom validation rules.
To use reactive forms in Ionic, you need to import the `ReactiveFormsModule` in your module, and then create a form using the `FormBuilder` service.
Here's an example of creating a reactive form in an Ionic component:
import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; ({ selector: 'app-login', templateUrl: './login.page.html', styleUrls: ['./login.page.scss'], }) export class LoginPage implements OnInit { loginForm: FormGroup; constructor(private fb: FormBuilder) {} ngOnInit() { this.loginForm = this.fb.group({ email: ['', [Validators.required, Validators.email]], password: ['', [Validators.required, Validators.minLength(6)]], }); } onSubmit() { console.log(this.loginForm.value); } }
In this example, we import the `FormBuilder` service and use it to create a `loginForm` object with two form controls: `email` and `password`. We set the initial values of these controls to empty strings, and provide validation rules using the `Validators` class.
In the HTML template for this component, we can bind the form controls to input elements using the `formControlName` directive:
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()"> <ion-item> <ion-label position="floating">Email</ion-label> <ion-input type="email" formControlName="email"></ion-input> </ion-item> <ion-item> <ion-label position="floating">Password</ion-label> <ion-input type="password" formControlName="password"></ion-input> </ion-item> <ion-button type="submit" [disabled]="!loginForm.valid">Log in</ion-button> </form>
In this template, we use the `formGroup` directive to bind the `loginForm` object to the form element. We use the `formControlName` directive to bind the form controls to the corresponding input elements. We also disable the "Log in" button if the form is not valid.
To provide custom validation rules, you can create your own validator functions and use them in the `Validators` array. For example, to validate that two password fields match, you could create a validator function like this:
function passwordMatchValidator(control: FormGroup) { const password = control.get('password').value; const confirmPassword = control.get('confirmPassword').value; return password === confirmPassword ? null : { passwordsDoNotMatch: true }; }
In this example, we provide the `passwordMatchValidator` function as an additional validator for the form group. This validator checks that the values of the `password` and `confirmPassword` controls match, and returns an error object if they don't.


