Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set custom global ErrorStateMatcher for lazy loaded modules #10084

Open
andrei-ilyukovich opened this issue Feb 22, 2018 · 15 comments
Open
Labels
area: material/core P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@andrei-ilyukovich
Copy link

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

@mmalerba mmalerba added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Feb 22, 2018
@shashikiran797
Copy link

@andrei-ilyukovich

Try this

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

@maxime1992
Copy link

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

@phsims
Copy link

phsims commented Oct 23, 2018

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

@tejas6jan
Copy link

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

@adnanmamajiwala
Copy link

adnanmamajiwala commented Nov 24, 2018

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.

@fortex
Copy link

fortex commented May 25, 2020

Still an issue in Angular 9 / Material 9.

The empty constructor @adnanmamajiwala mentioned does not work either.

@vmuresanu
Copy link

vmuresanu commented Sep 21, 2020

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.

@fwiesweg
Copy link

fwiesweg commented Feb 8, 2021

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.

@essana3
Copy link

essana3 commented Feb 11, 2021

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.

@gleisonkz
Copy link

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

@blockerdude
Copy link

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

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

@liesahead
Copy link

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

@liesahead
Copy link

Still relevant, up

@m4riok
Copy link

m4riok commented Dec 3, 2022

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.

@njsent
Copy link

njsent commented Mar 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/core P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests