Template-Driven Form With Validations in Angular Using VS Code and Node.js

Introduction 

 
In a template-driven approach, a template-driven form is the most common part of any application. Template-driven forms use the FormsModule to develop a template-driven form. A template-driven form is asynchronous in nature. In a this type of form, logic is created in the HTML component. The template-driven form is easy to use. Coding will be run on the HTML page, and validations of the forms are also performed on this page. Finally, the template-driven form uses NgForm and NgModule to develop a Form.
 

Features of template-driven forms 

  1. Easy to use
  2. Similar to Angular 1.0.
  3. Two-way data binding use
  4. Minimal component code
  5. Automatically tracks forms and data

NgForm

 
It is a directive that helps to create the control group inside the directives. It is attached to the element in HTML and supports the form tag along with some additional features.
 

NgModel

 
When we add NgModel Directive to the control, all the inputs are registered in the NgForm. When we use the NgModel with the form tag, or most importantly with the NgForm, we use the name property of Html controls. We are exporting ngForm in the local variable to use. Some of the properties of the forms and these properties are:
  1. regForm.value:- It gives the object containing all the values of the field in the form.
  2. regForm.valid:- This gives up the value indicating if the form is valid or not, If the value is valid is true or value is invalid is false.
  3. regForm.touched:- It returns true or false when one of the fields in the form is entered and touched.
Let's have look at the (ngSubmit) ="register(regForm)". Here we are using the event binding concept. Instead of Submit the event of the form ngSubmit which will send the actual Http request instead of just submitting the form.
 

Validation in Angular forms

 
Validations are an important aspect of programming. We can't always trust the user when we want to validate data.
 
Angular's set of common validators include min length, max-length, required email. We just need to add the validator directive to the control for assigning the controls to the validators.
 
The following are the classes:
  • ng-touched:- Controls have been visited
  • ng-untouched:- Controls have not to be visited
  • ng-dirty:- Controls value have been changed
  • ng-pristine:- Controls value have been changed
  • ng-valid:- Controls value are valid
  • ng-invalid:- Controls value is invalid
So in order to draw a border around the first name whenever the user visits any field without adding a value, we need to add these classes. Here is the code of the template-driven forms:
 
Appcomponent.css 
  1. .form-control{      
  2.     text-aligncenter;      
  3.     width200px;      
  4.     height40px;      
  5.     colorblack;      
  6.     border-colorblack;      
  7.     border-radius: 1px;      
  8. }      
  9. .btn-btn-primary{      
  10.     text-aligncenter;      
  11.     border-radius: 6px;      
  12.     background-color#1976d2;      
  13.     width80px;      
  14.     height:30px;      
  15.     border-stylehidden;      
  16.     colorwhite;      
  17. }      
  18. input.ng-invalid.ng-touched{      
  19.     border-colorred;      
  20. }      
  21. .text{      
  22.     text-aligncenter;      
  23.     font-sizex-large;      
  24. }      
  25. .container{      
  26.     text-aligncenter;      
  27. }   
Appcomponent.html
  1. <h1 class="text">{{title}}</h1>          
  2. <div class="container">           
  3.   <div class="row">          
  4.     <div class="form-bg">          
  5.       <form #regForm='ngForm' (ngSubmit)='Register(regForm)'>          
  6.         <h2 class="text-center">Registeration Page</h2>          
  7.       <br>          
  8.       <div class="form-group">           
  9.         <input type="text" class="form-control" placeholder="First name" name="firstname" required  ngModel>          
  10.       </div>          
  11.       <br>          
  12.       <div class="form-group">          
  13.         <input type="text" class="form-control" placeholder="Last name" name="lastname" required ngModel>          
  14.       </div>           
  15.       <br>          
  16.       <div class="form-group">          
  17.         <input type="email" class="form-control" placeholder="Email" name="email" email required ngModel #email='ngModel'>          
  18.         <span class="help-bpx" *ngIf="email.touched && !email.valid">Please enter email value</span>                       
  19.       </div>          
  20.        <br>          
  21.       <div class="button-control">          
  22.         <button type="submit" class="btn-btn-primary" id="register" [disabled]='!regForm.valid'>Register </button>          
  23.       </div>                
  24.                  
  25.       </form>          
  26.     </div>          
  27.   </div>          
  28. </div>     
appcomponent.ts
  1. import { Component } from '@angular/core';      
  2.       
  3. @Component({      
  4.   selector: 'app-root',      
  5.   templateUrl: './app.component.html',      
  6.   styleUrls: ['./app.component.css']      
  7. })      
  8. export class AppComponent {      
  9.   title = 'Template Driven-Forms';      
  10.   Register(regForm:any){      
  11.     debugger;      
  12.     var firstname=regForm.controls.firstname.value;      
  13.     var lastname=regForm.controls.lastname.value;      
  14.     var email=regForm.controls.lastname.value;      
  15.     console.log(regForm);      
  16.   }      
  17. }      
 module.ts
  1. import { BrowserModule } from '@angular/platform-browser';      
  2. import { NgModule } from '@angular/core';      
  3. import { AppComponent } from './app.component';      
  4. import {FormsModule} from '@angular/forms'      
  5. @NgModule({      
  6.   declarations: [      
  7.     AppComponent      
  8.   ],      
  9.   imports: [      
  10.     BrowserModule,FormsModule      
  11.   ],      
  12.   providers: [],      
  13.   bootstrap: [AppComponent]      
  14. })      
  15. export class AppModule { }    
Now compile this all components with command ng serve. After compiling all components successfully, open the browser and hit localhost:4200. The browser display output will look like this:

Template-Driven Form With Validations In Angular Using VS Code And Node.js
Now you can see that the register button is disabled. Fill the details in the form for checking the form validations. We left a few fields empty. Then see how the form looks:
 
Template-Driven Form With Validations In Angular Using VS Code And Node.js
After filling invalid data in the form, you can see the form fields show the color red in invalid Field and the register button is disabled. Fill all valid input in the form.
 Template-Driven Form With Validations In Angular Using VS Code And Node.js
After filling all valid info, you can see that the register button is enabled. Now you can see in the form that our form validations are performing their work properly. That means our form is complete with validations and ready to use.
 
Template-Driven Form With Validations In Angular Using VS Code And Node.js
Now you can see that the console didn't have any error and our form is ready to use. Now press the register button.
 
Template-Driven Form With Validations In Angular Using VS Code And Node.js
 
Now you can see that when the user clicks on the register button, then the console creates an object. Now click on the object and check it out.
 
Template-Driven Form With Validations In Angular Using VS Code And Node.js 
 
Now you can see when the user clicks on the object then object open no of the sub-object. Now click on the Form: 
 
Template-Driven Form With Validations In Angular Using VS Code And Node.js 
 
You can see when the user clicks on the form, it contains our valid data which we will be entered into the form. That means that our form is working properly.
 
I hope you enjoyed this article. To learn more logic and technology, follow C# Corner and follow my blog to learn Angular. 
 
Thank you!