Skip to content

Commit

Permalink
Set Error State Matcher from Control Model
Browse files Browse the repository at this point in the history
  • Loading branch information
jcroll committed Apr 30, 2020
1 parent 8ff9c59 commit f0d381a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit f0d381a

Please sign in to comment.