components icon indicating copy to clipboard operation
components copied to clipboard

Cannot set custom global ErrorStateMatcher for lazy loaded modules

Open andrei-ilyukovich opened this issue 7 years ago • 13 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

There should be ability to override ErrorStateMatcher for the whole application

What is the current behavior?

@NgModule({ providers: [ {provide: ErrorStateMatcher, useClass: CustomErrorStateMatcher} ] })

CustomErrorStateMatcher being provided in the application root module aren't applied inside of lazy loaded modules. Thus it is necessary to duplicate providing of custom error state matcher in every lazy loaded module.

What is the use-case or motivation for changing an existing behavior?

It is reasonable to have ability to override error state matcher bahavior for the whole app no matter it consists of lazy or non-lazy modules

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 5.1.3 Material 5.2.2

andrei-ilyukovich avatar Feb 22 '18 12:02 andrei-ilyukovich

@andrei-ilyukovich

Try this

@NgModule({ providers: [ {provide: ErrorStateMatcher, useValue: new CustomErrorStateMatcher()} ] })

shashikiran797 avatar Jun 05 '18 12:06 shashikiran797

@shashikiran797 this is not working and I don't see why it would

maxime1992 avatar Aug 14 '18 10:08 maxime1992

I have the same issue, did you get this fixed?

phsims avatar Oct 23 '18 13:10 phsims

Even I have the same issue, please let me know if anyone gets it fixed.

tejas6jan avatar Nov 01 '18 02:11 tejas6jan

I was facing the same issue, however after adding an empty constructor to the CustomErrorStateMatcher, it started working for me.

import {ErrorStateMatcher} from '@angular/material';
import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';

export class CustomErrorStateMatcher implements ErrorStateMatcher {

  constructor() {
  }

  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted = form && form.submitted;
    return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
  }

}

--

@NgModule({
  declarations: [...],
  imports: [...],
  providers: [
    {provide: ErrorStateMatcher, useClass: CustomErrorStateMatcher}
  ],
  bootstrap: [...]
})
export class AppModule {
}

Hope this helps.

adnanmamajiwala avatar Nov 24 '18 19:11 adnanmamajiwala

Still an issue in Angular 9 / Material 9.

The empty constructor @adnanmamajiwala mentioned does not work either.

fortex avatar May 25 '20 14:05 fortex

Issue still persists in Angular 10.1 Same for me, nothing from above proposed solutions work.

As a workaround I need to copy { provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher }, (or a custom error state matcher) in each and every lazy loaded module. Which basically in my case is not a big deal as I don't have so many lazy loaded modules, but still it is a workaround, and would be great to have an official solution taking into consideration that this comes since Angular 5.

vmuresanu avatar Sep 21 '20 15:09 vmuresanu

Angular Material 10.2.5 (with angular 10.1.6) exhibits similar behaviour, and we do have quite a few lazy-loaded modules and I will for sure forget the workaround for one of them sooner or later ;) So yes, this would be really lovely.

fwiesweg avatar Feb 08 '21 15:02 fwiesweg

Angular Material 10.2.5, same issue, I have 2 lazy loaded modules: Core and Backoffice. The ShowOnDirtyErrorStateMatcher works fine in the Backoffice but not in Core which is very weird.

essana3 avatar Feb 11 '21 00:02 essana3

I have the same issue, here using Angular 11 anyone have any solution?

gleisonkz avatar Mar 25 '21 13:03 gleisonkz

The solution is as @vmuresanu noted above. In each lazy loaded module you must provide the error state matcher

providers: [ { provide: ErrorStateMatcher, useClass: CustomErrorStateMatcher }, ]

blockerdude avatar Jul 14 '21 19:07 blockerdude

This issue needs some attention because validation is the thing that is used in most of the apps.

liesahead avatar Nov 10 '21 13:11 liesahead

Still relevant, up

liesahead avatar Sep 12 '22 10:09 liesahead

The solution is as @vmuresanu noted above. In each lazy loaded module you must provide the error state matcher

providers: [ { provide: ErrorStateMatcher, useClass: CustomErrorStateMatcher }, ]

Also works if provided in any module imported by all your lazy loaded modules. But still this need to be addressed especially since the best solution to #4190 is to use a different ErrorStateMatcher than the one provided.

m4riok avatar Dec 03 '22 08:12 m4riok

This issue also applies to standalone components. I have to import my custom ErrorStateMatcher (submitted = true) on each parent component.

njsent avatar Mar 08 '24 08:03 njsent