Learn full article on the Infragistics blog
In this blog post, we will learn to create custom validators in Angular Reactive Forms. If you are new to reactive forms, learn how to create your first Angular reactive form here.
Let’s say we have a login form as shown in the listing below. Currently, the form controls do not have any validations attached to it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.loginForm = new FormGroup({ | |
email: new FormControl(null), | |
password: new FormControl(null), | |
age : new FormControl(null) | |
}); | |
} |
Here, we are using FormGroup to create a reactive form. On the component template, you can attach loginForm as shown in the code listing below. Using property binding, the formGroup property of the HTML form element is set to loginForm and the formControlName value of these controls are set to the individual FormControl property of FormGroup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form (ngSubmit)='loginUser()' [formGroup]='loginForm' novalidate class="form"> | |
<input formControlName='email' type="text" class="form-control" placeholder="Enter Email" /> | |
<br/> | |
<input formControlName='password' type="password" class="form-control" placeholder="Enter Password" /> | |
<br/> | |
<input formControlName='age' type="number" class="form-control" placeholder="Enter Age" /> | |
<br/> | |
<button [disabled]='loginForm.invalid' class="btn btn-default">Login</button> | |
</form> |
This will give you a reactive form in your application:
Using Validators
Angular provides us many useful validators, including required, minLength, maxLength, and pattern. These validators are part of the Validators class, which comes with the @angular/forms package.
Let’s assume you want to add a required validation to the email control and a maxLength validation to the password control. Here’s how you do that:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ngOnInit() { | |
this.loginForm = new FormGroup({ | |
email: new FormControl(null, [Validators.required]), | |
password: new FormControl(null, [Validators.required, Validators.maxLength(8)]), | |
age : new FormControl(null) | |
}); | |
} |
To work with Validators, make sure to import them in the component class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FormGroup, FormControl, Validators } from '@angular/forms'; |
On the template, you can use validators to show or hide an error message. Essentially, you are reading the formControl using the get() method and checking whether it has an error or not using the hasError() method. You are also checking whether the formControl is touched or not using the touched property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input formControlName='email' type="text" class="form-control" placeholder="Enter Email" /> | |
<div class="alert alert-danger" *ngIf="loginForm.get('email').hasError('required') && loginForm.get('email').touched"> | |
Email is required | |
</div> |
If the user does not enter an email, then the reactive form will show an error as follows:
Custom Validator
Let us say you want the age range to be from 18 to 45. Angular does not provide us range validation; therefore, we will have to write a custom validator for this.
In Angular, creating a custom validator is as simple as creating another function. The only thing you need to keep in mind is that it takes one input parameter of type AbstractControl and it returns an object of key value pair if the validation fails.
Let’s create a custom validator called ageRangeValidator, where the user should able to enter an age only if it’s in a given range.
The type of the first parameter is AbstractControl because it is a base class of FormControl, FormArray, and FormGroup, and it allows you to read the value of the control passed to the custom validator function. The custom validator returns either of the following:
1. If the validation fails, it returns an object, which contains a key value pair. Key is the name of the error and the value is always Boolean true.
2. If the validation does not fail, it returns null.
Leave a Reply