State And Validation Tracking In Angular

Introduction

 
In this article, we will see how Angular tracks the state of the form control and applies the necessary class that helps us know what validation has been applied. Validation and applying the appropriate visual feedback for the controls are very important for any application. Now, let us see how Angular applies the class based on the state of control and validity.
 

Prerequisites

 
Basic knowledge of Angular classes and components.
 
Angular provides three pairs of classes at any point of the state of the control.
 
ng-touched and ng-untouched
This pair of classes defines the state of the control whether it has been touched or not.
 
ng-touched will be applied if the condition is true and ng-untouched will be applied if false.
 
ng-dirty and ng-pristine
This pair of classes defines the state of the control whether its value has been changed or not.
 
ng-dirty will be applied if the condition is true and ng-pristine will be applied if false.
 
ng-valid and ng-invalid
This pair of classes defines the state of the control whether its value is valid or not.
 
ng-valid will be applied if the condition is true and ng-invalid will be applied if false.
 
Let’s see how these classes apply to our application by giving the necessary state.
 
Before we start to see how the classes are being applied, click here to create the sample application on which we will see how the classes are being applied.
 
Open app.component.html and modify the below set of HTML code. 
  1. <div class="col-sm-4">  
  2.           <div class="form-group">  
  3.               <label for="name">Name:</label>  
  4.               <input type="text" #controlName class="form-control" [ngModel]="studentModel.name" name="name">  
  5.             </div>  
  6.             <div>  
  7.               <b>class applied on 'Name' control : <i>{{controlName.className}}</i></b>  
  8.             </div>  
  9.       </div>  
Reference the variable #controlName for the control ‘Name’ and echo the classes applied on the same control.
  1. Applied ng-touched and ng-untouched classes.
    Run the application and see the classes applied on ‘name’ control.

    State And Validation Tracking In Angular

    ng-untouched is applied because the control has not been visited. We didn’t touch the control yet.

    When you just click on the control and move to any other, then the ng-touched class will be applied.

    State And Validation Tracking In Angular
  1. Applied ng-dirty and ng-pristine classes.
    When we load the form classes ‘form-control ng-untouched ng-prestine ng-valid’ classes has been applied.

    When you focus and edit some values then the ng-prestine class will be removed and ng-dirty will be applied.
  1. Applied ng-valid and ng-invalid classes.

    These two classes will be applied on the basis of value whether it’s valid or invalid.

    Let’s add a simple required attribute to make the field value valid/invalid.
    1. <input type="text" #controlName required class="form-control [ngModel]="studentModel.name" name="name">  
Run the application.
 
Focus to the control ‘name’ and delete the value or make the value blank for the control.
 
State And Validation Tracking In Angular
 
We can apply different validation based on value, email, phone number, pincode etc.
 
For each of the classes, Angular provides an associative Angular property on the ng model directives. The ngModel properties are as follows:
 
Class
Property
ng-untouched
untouched
ng-touched
touched
ng-prestine
pristine
ng-dirty
dirty
ng-valid
valid
ng-invalid
invalid
 
We can access these properties with the help of ngModel directive reference.
 
Add the below set of code and save the changes. 
  1. <div class="col-sm-4">  
  2.           <div class="form-group">  
  3.               <label for="name">Name:</label>  
  4.               <input type="text" #controlName='ngModel' required class="form-control" [ngModel]="studentModel.name" name="name">  
  5.             </div>  
  6.             <div>  
  7.               <b>class applied on 'Name' control 'touched' : <i>{{controlName.touched}}</i></b><br>  
  8.               <b>class applied on 'Name' control 'untouched' : <i>{{controlName.untouched}}</i></b><br>  
  9.               <b>class applied on 'Name' control 'pristine' : <i>{{controlName.pristine}}</i></b><br>  
  10.               <b>class applied on 'Name' control 'dirty' : <i>{{controlName.dirty}}</i></b><br>  
  11.               <b>class applied on 'Name' control 'valid' : <i>{{controlName.valid}}</i></b><br>  
  12.               <b>class applied on 'Name' control 'invalid' : <i>{{controlName.invalid}}</i></b>  
  13.             </div>  
  14.       </div>  
Run the application.
 
State And Validation Tracking In Angular
 
You can see the property return the Boolean value and it is much better to work with applied classes than the class name.