2018-09-18 15:51:57 +02:00
|
|
|
import { FormControl, FormGroupDirective, NgForm } from '@angular/forms';
|
2019-07-26 11:46:59 +02:00
|
|
|
import { ErrorStateMatcher } from '@angular/material/core';
|
2018-09-18 15:51:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom state matcher for mat-errors. Enables the error for an input, if one has set the error
|
|
|
|
* with `setError` on the parent element.
|
|
|
|
*/
|
|
|
|
export class ParentErrorStateMatcher implements ErrorStateMatcher {
|
|
|
|
public isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
|
|
|
|
const isSubmitted = !!(form && form.submitted);
|
|
|
|
const controlTouched = !!(control && (control.dirty || control.touched));
|
|
|
|
const controlInvalid = !!(control && control.invalid);
|
|
|
|
const parentInvalid = !!(
|
|
|
|
control &&
|
|
|
|
control.parent &&
|
|
|
|
control.parent.invalid &&
|
|
|
|
(control.parent.dirty || control.parent.touched)
|
|
|
|
);
|
|
|
|
|
|
|
|
return (isSubmitted || controlTouched) && (controlInvalid || parentInvalid);
|
|
|
|
}
|
|
|
|
}
|