Reactive Forms And Form Validation In Angular With Example

This article explains how to use Angular reactive forms. We will walk through how to set up an angular form with form validation. we learn angular forms example.

We see an example of angular reactive forms validation. I explained simply step by step angular form validation. Follow the below step for the angular form validation example.

The reactive form provides a model-driven approach to handling form inputs value change over time. In this form reactive form, we need to import "ReactiveFormsModule" from the angular forms library. We will use FormControl, FormGroup, FormArray, Validation class with Reactive forms in angular.

We and you have a simple and basic form in an angular application then I will prefer to use Reactive forms in angular we learn angular forms example. here I write a simple example of Reactive forms with validation in angular.

You need to follow the below step to create reactive forms in angular.

Reactive Forms And Form Validation In Angular With Example

Step 1: Install Angular App

Here, in this step, you need to create a new ng-app for this demo. if you have already created then don't create a new angular.

Step 2: Import FormsModule

If you want to create a form in the angular app then you need to import FormsModule from @angular/forms library. So let's add the following code to the app.module.ts file.


import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';  // <----------------- This code---------------
import { AppComponent } from './app.component';
  declarations: [
  imports: [
    FormsModule, // <----------------- This code---------------
    ReactiveFormsModule // <----------------- This code---------------
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }

Step 3: Form with ngModel

In this step, we will write code of HTML form with ngModel. So add the following code to the app.component.html file.


<h1>Reactive Forms And Form Validation In Angular With Example -</h1>
<form [formGroup]="form" (ngSubmit)="submit()">
    <div class="form-group">
        <label for="name">Name</label>
        <input formControlName="name" id="name" type="text" class="form-control">
        <span *ngIf=" &&" class="text-danger">Name is required.</span>
    <div class="form-group">
        <label for="email">Email</label>
        <input formControlName="email" id="email" type="text"  class="form-control">
        <span *ngIf=" &&" class="text-danger">Email is required.</span>
    <div class="form-group">
        <label for="body">Body</label>
        <textarea formControlName="body" id="body" type="text" class="form-control">  </textarea>
          <span *ngIf="form.body.touched && form.body.invalid" class="text-danger">Body is required.</span>
    <button class="btn btn-primary" type="submit">Submit</button>

Step 4: updated Ts File

In this file, we will write submit() and get all input field values. so let's add the following code to the app.component.ts file.


import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators} from '@angular/forms';     // <----------------- This code---------------
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent {
    constructor() { }

  ngOnInit(): void {


  form = new FormGroup({
    name: new FormControl('', Validators.required),
    email: new FormControl('', Validators.required),
    body: new FormControl('', Validators.required)

Now you can run your application using the following command:

ng serve


{name: "Robert Look", email: "[email protected]", body: "Welcome to Php Coding Stuff"}


I learned in an angular reactive form that how we do form validation in Angular reactive form and make it very easy. We learn angular forms example.

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!