Custom Validator In Reactive Forms In Angular
In this article, you'll learn how to create a custom validator in Angular Reactive form. A significant number of fields can be found on a data entry form. The Angular forms module makes creating, managing, and validating form fields more easier. In Angular, you can design forms in two different methods. The first are reactive forms, while the second are template-driven forms. Learn how to make reactive forms as well as template-driven forms.
Built-in Validators
Validating the forms is critical; otherwise, we would end up with inaccurate data in our database. To assist us in validating the form, the Angular Forms Module includes a few built-in validators. The following is a list of them.
- Required validator
- Min length Validator
- Max length Validator
- Pattern Validator
- Email Validator
Custom Validator in Angular Reactive Form
Built-in validators are helpful, but they don't cover every scenario. This is when the custom validator comes in handy. In Angular, creating a custom validator is a breeze.
How to Build Custom Validator
It's just as simple to create a custom Validator as it is to create a Validator function. It's a function that must conform to the ValidatorFn Interface.
ValidatorFn
The ValidatorFn is an Interface that defines the Validator function's signature.
interface ValidatorFn { (control: AbstractControl): ValidationErrors | null }
Custom Validator Example
<h1>Custom Validator in Angular</h1> <h2>Reactive Form</h2> <form formgroup="" myform="" ngsubmit="" novalidate="" onsubmit=""> <div> <label>Number :</label> <input formcontrolname="numVal" id="numVal" name="numVal" type="text" /> </div> <p>Is Form Valid : {{myForm.valid}} </p> <p> <button disabled="" myform.valid="" type="submit">Submit</button> </p> </form>
import { AbstractControl, ValidationErrors } from '@angular/forms' export function gte(control: AbstractControl): ValidationErrors | null { const v=+control.value; if (isNaN(v)) { return { 'gte': true, 'requiredValue': 10 } } if (v <= 10) { return { 'gte': true, 'requiredValue': 10 } } return null }
To begin, import the AbstractControl and ValidationErrors classes from the @angular/forms package.
import { AbstractControl, ValidationErrors } from '@angular/forms'
export function gte(control: AbstractControl): ValidationErrors | null {
const v=+control.value; if (isNaN(v)) { return { 'gte': true, 'requiredValue': 10 } } if (v <= 10) { return { 'gte': true, 'requiredValue': 10 } } return null
return { 'gte': true, 'requiredValue': 10 }
Using the Custom Validator
import { gte } from './gte.validator';
myForm = new FormGroup({ numVal: new FormControl('', [gte]), })
Below is the entire app.component.ts file.
import { Component } from '@angular/core'; import { FormGroup, FormControl, AbstractControl, ValidationErrors } from '@angular/forms' import { gte } from './gte.validator'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { constructor() { } myForm = new FormGroup({ numVal: new FormControl('', [gte]), }) get numVal() { return this.myForm.get('numVal'); } onSubmit() { console.log(this.myForm.value); } }
Accessing the Errors from Custom Validator
<div> <label>Number :</label> <input formcontrolname="numVal" id="numVal" name="numVal" type="text" /> <div ngif="!numVal.valid && (numVal.dirty ||numVal.touched)"> <div ngif="numVal.errors.gte"> The number should be greater than {{numVal.errors.requiredValue}} </div> </div> </div>
<div ngif="numVal.errors.gte"> The number should be greater than {{numVal.errors.requiredValue}} </div>