Last active
May 19, 2020 06:43
-
-
Save xiongemi/fd3312534f7132d8a6833be99a2837cc to your computer and use it in GitHub Desktop.
Angular Form Group Error State Matcher
This file contains hidden or 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 { FormControl, FormGroupDirective, NgForm } from '@angular/forms'; | |
import { ErrorStateMatcher } from '@angular/material/core'; | |
export class FormGroupErrorStateMatcher implements ErrorStateMatcher { | |
errors: string[]; | |
constructor(errors: string[]) { | |
this.errors = errors; | |
} | |
isErrorState(control: FormControl | null, formDirective: FormGroupDirective | NgForm | null): boolean { | |
const formGroupHasError = this.errors.reduce((total, error) => total || formDirective.form.hasError(error), false); | |
return control.touched && (control.invalid || formGroupHasError); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment