Skip to content

Commit f0d381a

Browse files
committed
Set Error State Matcher from Control Model
1 parent 8ff9c59 commit f0d381a

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,17 @@ export const DEFAULT_ERROR_STATE_MATCHER: DynamicErrorMessagesMatcher =
10711071
};
10721072
```
10731073

1074+
You can also set an error matcher on a per control basis by assigning it under the `additional` field:
1075+
```ts
1076+
new DynamicInputModel({
1077+
id: "sampleInput",
1078+
label: "Sample Input",
1079+
additional: {
1080+
errorStateMatcher: myCustomErrorMessagesMatcher
1081+
}
1082+
})
1083+
```
1084+
10741085
Please note here that NG Dynamic Forms always assumes both the control being invalid and error messages being defined on the model
10751086
as a fixed precondition.
10761087

projects/ng-dynamic-forms/core/src/lib/service/dynamic-form-validation.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,18 @@ export class DynamicFormValidationService {
136136
control.updateValueAndValidity();
137137
}
138138

139-
showErrorMessages(control: AbstractControl, model: DynamicFormControlModel, hasFocus: boolean): boolean {
140-
141-
const precondition = control.invalid && model.hasErrorMessages;
142-
const matcher = this._DYNAMIC_ERROR_MESSAGES_MATCHER ? this._DYNAMIC_ERROR_MESSAGES_MATCHER(control, model, hasFocus) :
143-
DEFAULT_ERROR_STATE_MATCHER(control, model, hasFocus);
144-
145-
return precondition && matcher;
146-
}
139+
showErrorMessages(control: AbstractControl, model: DynamicFormControlModel, hasFocus: boolean): boolean {
140+
const precondition = control.invalid && model.hasErrorMessages;
141+
142+
const matcher =
143+
typeof model["getAdditional"] !== undefined && model["getAdditional"]("errorStateMatcher")
144+
? model["getAdditional"]("errorStateMatcher")
145+
: this._DYNAMIC_ERROR_MESSAGES_MATCHER
146+
? this._DYNAMIC_ERROR_MESSAGES_MATCHER
147+
: DEFAULT_ERROR_STATE_MATCHER;
148+
149+
return precondition && matcher(control, model, hasFocus);
150+
}
147151

148152
parseErrorMessageConfig(template: string, model: DynamicFormControlModel, error: any = null): string {
149153

0 commit comments

Comments
 (0)