Skip to content

Instantly share code, notes, and snippets.

@xiongemi
Last active May 19, 2020 06:43
Show Gist options
  • Save xiongemi/fd3312534f7132d8a6833be99a2837cc to your computer and use it in GitHub Desktop.
Save xiongemi/fd3312534f7132d8a6833be99a2837cc to your computer and use it in GitHub Desktop.
Angular Form Group Error State Matcher
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