![]() |
VOOZH | about |
In Angular 2, the best way to deal with complex forms is by using Reactive forms. Below we are going to elaborate on how to trigger form validators for login page.
In reactive forms, we use FormControl and by using this, we get access to sub-fields of form and their properties. Some of their properties are dirty, touched, untouched, pristine, valid, errors and etc. Using these properties, we can actually trigger validations according to the requirement.
Using the above-discussed properties, we can trigger the form validations with custom messages. Using the FormControl of every sub-field and checking its properties like touched, dirty and etc, we can validate the form according to the need.
Approach:
For better animations and styling angular provides Angular material which has abundant information regarding the styling. Using angular material, the form is styled. Hence, we are using tags like <mat-form-field>, <mat-error>, <mat-label> and matInput.
We can import from angular material after installing it using npm. The command for importing it is mentioned below:
ng add @angular/material
Code Implementation: Below is the implementation of the above approach.
app.module.ts:
app.component.ts:
app.component.html:
Output: Hence, we have successfully triggered form validations.
👁 Image