From f0d381ac081f7068ae917846181f431568e03197 Mon Sep 17 00:00:00 2001 From: Jarrett Croll Date: Thu, 30 Apr 2020 15:05:33 -0400 Subject: [PATCH] Set Error State Matcher from Control Model --- README.md | 11 ++++++++++ .../dynamic-form-validation.service.ts | 20 +++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 282107a39..ec4b2c218 100644 --- a/README.md +++ b/README.md @@ -1071,6 +1071,17 @@ export const DEFAULT_ERROR_STATE_MATCHER: DynamicErrorMessagesMatcher = }; ``` +You can also set an error matcher on a per control basis by assigning it under the `additional` field: +```ts +new DynamicInputModel({ + id: "sampleInput", + label: "Sample Input", + additional: { + errorStateMatcher: myCustomErrorMessagesMatcher + } +}) +``` + Please note here that NG Dynamic Forms always assumes both the control being invalid and error messages being defined on the model as a fixed precondition. diff --git a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-validation.service.ts b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-validation.service.ts index 410c57d12..ab3e7793a 100644 --- a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-validation.service.ts +++ b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-validation.service.ts @@ -136,14 +136,18 @@ export class DynamicFormValidationService { control.updateValueAndValidity(); } - showErrorMessages(control: AbstractControl, model: DynamicFormControlModel, hasFocus: boolean): boolean { - - const precondition = control.invalid && model.hasErrorMessages; - const matcher = this._DYNAMIC_ERROR_MESSAGES_MATCHER ? this._DYNAMIC_ERROR_MESSAGES_MATCHER(control, model, hasFocus) : - DEFAULT_ERROR_STATE_MATCHER(control, model, hasFocus); - - return precondition && matcher; - } + showErrorMessages(control: AbstractControl, model: DynamicFormControlModel, hasFocus: boolean): boolean { + const precondition = control.invalid && model.hasErrorMessages; + + const matcher = + typeof model["getAdditional"] !== undefined && model["getAdditional"]("errorStateMatcher") + ? model["getAdditional"]("errorStateMatcher") + : this._DYNAMIC_ERROR_MESSAGES_MATCHER + ? this._DYNAMIC_ERROR_MESSAGES_MATCHER + : DEFAULT_ERROR_STATE_MATCHER; + + return precondition && matcher(control, model, hasFocus); + } parseErrorMessageConfig(template: string, model: DynamicFormControlModel, error: any = null): string {