2021-05-11 13:34:23 +02:00
|
|
|
// Core imports
|
2021-05-10 18:15:25 +02:00
|
|
|
import { ErrorStateMatcher } from '@angular/material/core';
|
|
|
|
import { FormControl, FormGroupDirective, NgForm } from '@angular/forms';
|
2020-12-28 10:06:30 +01:00
|
|
|
|
2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Custom provider that defines how form controls behave with regards to displaying error messages.
|
|
|
|
*
|
|
|
|
*/
|
2021-05-10 18:15:25 +02:00
|
|
|
export class CustomErrorStateMatcher implements ErrorStateMatcher {
|
2021-05-11 13:34:23 +02:00
|
|
|
/**
|
|
|
|
* Checks whether an invalid input has been made and an error should be made.
|
|
|
|
*
|
|
|
|
* @param control - Tracks the value and validation status of an individual form control.
|
|
|
|
* @param form - Binding of an existing FormGroup to a DOM element.
|
|
|
|
* @returns true - If an invalid input has been made to the form control.
|
|
|
|
*/
|
2020-12-28 10:06:30 +01:00
|
|
|
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
|
2021-04-29 19:10:39 +02:00
|
|
|
const isSubmitted: boolean = form && form.submitted;
|
2020-12-28 10:06:30 +01:00
|
|
|
return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
|
|
|
|
}
|
|
|
|
}
|