Â
Template Driven Form and Reactive Form
Table of Contents
- Why not Template Driven Forms? Why Reactive Forms?Â
- What is Fluent Validation?
Why not Template Driven Forms? Why Reactive Forms?Â
Template-driven forms are:
- Contains little code in the component class as its logic contains in (template) html.
- Easier to set up .
While they are:
- Difficult to add controls dynamically.
- Unit testing is a challenge because logic are in template(html) not in typescript.
- Their readability will quickly drop as we add more and more validators and input tags; keeping all their logic within the template might be fine for small forms, but it doesn't scale well when dealing with complex data items.
Reactive Forms are:
- More flexible than template driven form.
- Handles any complex scenarios.
- No data binding is done (immutable data model preferred by most developers).
- More component code and less HTML markup.
- Easier unit testing.
- Model-driven(Reactive) forms are used in creating medium to large scale applications.
- It provides flexibility to add client-side validation.
- Moreover we can create custom validator.
Open app.module.ts and below code to add a reactive form module:
import { BrowserModule } from '@angular/platform-browser';Â Â
import { NgModule } from '@angular/core';Â Â
import { ReactiveFormsModule } from '@angular/forms';Â Â
import { AppRoutingModule } from './app-routing.module';Â Â
import { AppComponent } from './app.component';Â Â
import { RegistrationFormComponent } from './registration-form/registration-form.component';Â Â
@NgModule({Â Â
 declarations: [ Â
  AppComponent, Â
 ], Â
 imports: [ Â
  BrowserModule, Â
  AppRoutingModule, Â
  ReactiveFormsModule Â
 ], Â
 providers: [], Â
 bootstrap: [AppComponent] Â
})Â Â
export class AppModule { }Â Â
After that you can use in component.ts file where you want to use reactive forms:
import { Component, OnInit } from '@angular/core';Â Â
import { FormBuilder, FormGroup, Validators } from '@angular/forms';Â Â
@Component({Â Â
 selector: 'app-registration-form', Â
 templateUrl: './registration-form.component.html', Â
 styleUrls: ['./registration-form.component.css'] Â
})Â Â
export class RegistrationFormComponent implements OnInit {Â Â
 registerForm: FormGroup;
 submitted : Boolean;
 constructor(private formBuilder: FormBuilder) { } Â
 ngOnInit() { Â
  this.registerForm = this.formBuilder.group({ Â
   firstName: ['', Validators.required], Â
   lastName: ['', Validators.required], Â
   address: this.formBuilder.group({ Â
    street: [], Â
    zip: [], Â
   }) Â
  }); Â
 } Â
}Â Â
In html:
<div class="container">Â Â
  <h1>Registration Form</h1> Â
  <form [formGroup]="registerForm"> Â
   <div class="form-group"> Â
    <label>First Name</label> Â
    <input type="text" class="form-control" formControlName="firstName"> Â
    <p *ngIf="registerForm.controls.firstName.errors" class="alert alert-danger">This field is required!</p> Â
   </div> Â
   <div class="form-group"> Â
    <label>Last Name</label> Â
    <input type="text" class="form-control" formControlName="lastName"> Â
    <p *ngIf="registerForm.controls.lastName.errors" class="alert alert-danger">This field is required!</p> Â
   </div> Â
   <div class="form-group"> Â
    <fieldset formGroupName="address"> Â
     <label>Street</label> Â
     <input type="text" class="form-control" formControlName="street"> Â
     <label>Zip</label> Â
     <input type="text" class="form-control" formControlName="zip"> Â
     <label>City</label> Â
     <input type="text" class="form-control" formControlName="city"> Â
    </fieldset> Â
   </div> Â
   <button type="submit" (click)="submitted=true">Submit</button> Â
  </form> Â
 <br/> Â
  <div [hidden]="!submitted"> Â
   <h3> Employee Details </h3> Â
   <p>First Name: {{ registerForm.get('firstName').value }} </p> Â
   <p> Last Name: {{ registerForm.get('lastName').value }} </p> Â
   <p> Street: {{ registerForm.get('address.street').value }}</p> Â
   <p> Zip: {{ registerForm.get('address.zip').value }} </p> Â
  </div> Â
 </div> Â
  Â
What is Fluent Validation?
It is a server side validation. Some validation needs to be checked from the database. Previously there was a validation but users were not getting proper error messages. So to address this issue, we are using fluent validation. There are built-in validators as well as we can create our own custom validators. With the help of fluent validation, we can send user a custom error messages and easily notify about the error.