Search code examples
angularangular-reactive-formsangular-formsangular2-form-validationngneat

Customize third party directive's implementation without forking


In our Angular project we are using @ngneat/error-tailor for showing form errors: We are interested in showing the error messages when the controls are touched, feature which is requested in this github issue. There is even a PR involving this change, but with no activity since May 2024.

We are wondering: Would it be possible to override in our project only ControlErrorDirective's behaviour without forking the whole library, so we could in the near future opt-out for this override? If so, how?


Solution

  • Just copy the source code (lib) to your project in it's own separate folder, your application can use this code like below.

    import { provideErrorTailorConfig } from './error-tailor/lib/error-tailor.providers';
    
    bootstrapApplication(AppComponent, {
      providers: [
        provideErrorTailorConfig({
          errors: {
            useValue: {
              required: 'This field is required',
              minlength: ({ requiredLength, actualLength }) => 
                          `Expect ${requiredLength} but got ${actualLength}`,
              invalidAddress: error => `Address isn't valid`
            }
          }
        })
      ]
    })