question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot set custom global ErrorStateMatcher for lazy loaded modules

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:13
  • Comments:14

github_iconTop GitHub Comments

9reactions
adnanmamajiwalacommented, 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.

5reactions
fortexcommented, May 25, 2020

Still an issue in Angular 9 / Material 9.

The empty constructor @adnanmamajiwala mentioned does not work either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom ErrorStateMatcher does not work when provided on ...
I want to implement a custom ErrorStateMatcher to show errors even when the formControl is not toched for a matInput from a 3rd...
Read more >
Lazy-loading feature modules - Angular
Setting up a lazy-loaded feature module requires two main steps: Create the feature module with the Angular CLI, using the --route flag. Configure...
Read more >
mat-error shows only when from-control was touched-angular.js
Create a custom ErrorStateMatcher · Declare a filed of the newly created type in your component code · Refer to the custom error...
Read more >
Angular Forms and Validations
We wanted to create the most complete angular forms tutorial, so we also added advanced custom validators. If this is your first time...
Read more >
Lazy Loading | webpack
We'll add an interaction to log some text to the console when the user clicks a button. However, we'll wait to load that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found