How to create Reactive Form with Validation in Angular 8 Application?

How to create Reactive Form with Validation in Angular 8 Application?

In this Angular tutorial, we will create reactive form step by step with validation like required, minlength, email etc with Angular 8 Application.

In this tutorial, I would like to give you example of Angular 8 reactive forms validation. I will study example of reactive form validation in Angular 8 example. You will learn to create reactive form in Angular 8 Application.

We will create reactive form step by step with validation like required, minlength, email etc with Angular 8 Application.

Reactive forms provide a model-driven approach to handling form inputs whose values change over the time. In Reactive forms, we need to import "ReactiveFormsModule" from angular forms library. we will use FormControl, FormGroup, FormArray, Validation classes with Reactive forms in angular 8 app.

If you have simple and basic form in your Angular 8 Application then i will prefer to use Reactive forms in Angular. Here i write simple example of Reactive forms with validation in Angular 8.

You need to follow bellow step to create reactive forms in Angular 8.

Step 1: Install Angular App

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

ng new my-new-app

Step 2: Import FormsModule

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

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

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



import { AppComponent } from './app.component';



@NgModule({

  declarations: [

    AppComponent

  ],

  imports: [

    BrowserModule,

    FormsModule,

    ReactiveFormsModule

  ],

  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 following code to app.component.html file.

I used bootstrap class on this form.

<h1>Reactive Form with Validation in Angular 8 - ItSolutionStuff.com</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">

        <div *ngIf="f.name.touched && f.name.invalid" class="alert alert-danger">

            <div *ngIf="f.name.errors.required">Name is required.</div>

            <div *ngIf="f.name.errors.minlength">Name should be 3 character.</div>

        </div>

    </div>



    <div class="form-group">

        <label for="email">Email</label>

        <input 

            formControlName="email"

            id="email" 

            type="text" 

            class="form-control">

        <div *ngIf="f.email.touched && f.email.invalid" class="alert alert-danger">

            <div *ngIf="f.email.errors.required">Email is required.</div>

            <div *ngIf="f.email.errors.email">Please, enter valid email address.</div>

        </div>

    </div>



    <div class="form-group">

        <label for="body">Body</label>

        <textarea 

            formControlName="body"

            id="body" 

            type="text" 

            class="form-control">

        </textarea>

        <div *ngIf="f.body.touched && f.body.invalid" class="alert alert-danger">

            <div *ngIf="f.body.errors.required">Body is required.</div>

        </div>

    </div>



    <button class="btn btn-primary" type="submit">Submit</button>

</form>

Step 4: updated Ts File

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

import { Component } from '@angular/core';

import { FormGroup, FormControl, Validators} from '@angular/forms';



@Component({

  selector: 'app-root',

  templateUrl: './app.component.html',

  styleUrls: ['./app.component.css']

})

export class AppComponent {



  form = new FormGroup({

    name: new FormControl('', [Validators.required, Validators.minLength(3)]),

    email: new FormControl('', [Validators.required, Validators.email]),

    body: new FormControl('', Validators.required)

  });



  get f(){

    return this.form.controls;

  }



  submit(){

    console.log(this.form.value);

  }



}

Now you can run your application using following command:

ng serve

I hope it can help you...

Angular JavaScript WebDev

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc

Upgrade Angular CLI from Angular 8 to Angular 9

In this Angular 9 tutorial is focused on upgrade Angular CLI to 9 in Ubuntu. This example will help you upgrade Angular CLI version for Angular 8 to Angular 9 globally. This article will give you simple example of update Angular 8 to 9. We will look at example of upgrade angular 8 to 9. Let's see bellow example update Angular CLI version 8 to 9.

How to Build an Angular Application with Angular CLI

How to set up the Angular CLI and generate a Trivial App

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project

ReactJS vs Angular vs Vue: Best Javascript Framework For Your Project. This video covers the key differences between ReactJS, Angular and Vue with respect to the following: Use case, Performance, Data binding, Scripting language, Testing, Community support, Growth curve