import { Component, OnInit } from '@angular/core'; import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/forms'; import {ErrorStateMatcher, ShowOnDirtyErrorStateMatcher} from '@angular/material/core'; export class MyErrorStateMatcher implements ErrorStateMatcher { isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { const isSubmitted = form && form.submitted; return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted)); } } @Component({ selector: 'app-test-custom-theme', templateUrl: './test-custom-theme.component.html', styleUrls: ['./test-custom-theme.component.scss'], providers: [ { provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher } ] }) export class TestCustomThemeComponent implements OnInit { emailFormControl = new FormControl('', [ Validators.required, Validators.email, ]); matcher = new MyErrorStateMatcher(); constructor() { } ngOnInit() { } }