Custom validation
In the previous section, we saw how to use validators and combine validators together to create more complex validation. The Validators.required
, Validators.minLength
, Validators.maxLength
, and Validators.pattern
combinations can cover a lot of validation cases that can arise during the development of your Angular application. If the time comes where you can't handle your validation needs with the built-in validator, then you can build your very own validator.
In this section, we'll see how to validate that the movie_id
field contains a valid entry (that is a number that is between one and four digits long) and that another movie does not already use the id. To do so, we can create the following class:
import { FormControl } from '@angular/forms'; interface ValidationResult { [key:string]:boolean; } export class MovieIDValidator{ static idNotTaken(control: FormControl): ValidationResult { let movies = require('./marvel-cinematic- universe...