@lagoshny/ngx-validation-messages 3.0.0 → 16.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +427 -426
  3. package/{esm2020 → esm2022}/lagoshny-ngx-validation-messages.mjs +4 -4
  4. package/esm2022/lib/components/ngx-custom-message/ngx-custom-message.component.mjs +40 -0
  5. package/esm2022/lib/components/ngx-validation-messages/ngx-validation-messages.component.mjs +112 -0
  6. package/esm2022/lib/directivies/ngx-validator-name.directive.mjs +38 -0
  7. package/esm2022/lib/interface/ngx-validation-messages.config.mjs +3 -0
  8. package/esm2022/lib/ngx-validation-messages.module.mjs +56 -0
  9. package/esm2022/lib/service/ngx-validation-messages.service.mjs +73 -0
  10. package/{esm2020 → esm2022}/public-api.mjs +5 -5
  11. package/{fesm2020 → fesm2022}/lagoshny-ngx-validation-messages.mjs +284 -253
  12. package/fesm2022/lagoshny-ngx-validation-messages.mjs.map +1 -0
  13. package/{lagoshny-ngx-validation-messages.d.ts → index.d.ts} +5 -5
  14. package/lib/components/ngx-custom-message/ngx-custom-message.component.d.ts +20 -20
  15. package/lib/components/ngx-validation-messages/ngx-validation-messages.component.d.ts +40 -40
  16. package/lib/directivies/ngx-validator-name.directive.d.ts +25 -25
  17. package/lib/interface/ngx-validation-messages.config.d.ts +17 -17
  18. package/lib/ngx-validation-messages.module.d.ts +18 -18
  19. package/lib/service/ngx-validation-messages.service.d.ts +37 -37
  20. package/package.json +8 -13
  21. package/public-api.d.ts +1 -1
  22. package/esm2020/lib/components/ngx-custom-message/ngx-custom-message.component.mjs +0 -32
  23. package/esm2020/lib/components/ngx-validation-messages/ngx-validation-messages.component.mjs +0 -97
  24. package/esm2020/lib/directivies/ngx-validator-name.directive.mjs +0 -29
  25. package/esm2020/lib/interface/ngx-validation-messages.config.mjs +0 -3
  26. package/esm2020/lib/ngx-validation-messages.module.mjs +0 -58
  27. package/esm2020/lib/service/ngx-validation-messages.service.mjs +0 -72
  28. package/fesm2015/lagoshny-ngx-validation-messages.mjs +0 -282
  29. package/fesm2015/lagoshny-ngx-validation-messages.mjs.map +0 -1
  30. package/fesm2020/lagoshny-ngx-validation-messages.mjs.map +0 -1
@@ -5,273 +5,304 @@ import { Component, Input, ViewChild, Directive, InjectionToken, Injectable, Inj
5
5
  import * as i3 from '@angular/material/form-field';
6
6
  import { MatFormFieldModule } from '@angular/material/form-field';
7
7
 
8
- /**
9
- * Component allows specifying custom validation message for the specified
10
- * validator to override default message.
11
- *
12
- * Use this component as child in {@link NgxValidationMessagesComponent}.
13
- */
14
- class NgxCustomMessageComponent {
15
- }
16
- NgxCustomMessageComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxCustomMessageComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
17
- NgxCustomMessageComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.1.1", type: NgxCustomMessageComponent, selector: "ngx-custom-message", inputs: { validatorName: ["forValidator", "validatorName"] }, viewQueries: [{ propertyName: "message", first: true, predicate: ["message"], descendants: true }], ngImport: i0, template: `
18
- <div #message>
19
- <ng-content></ng-content>
20
- </div>`, isInline: true });
21
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxCustomMessageComponent, decorators: [{
22
- type: Component,
23
- args: [{
24
- selector: 'ngx-custom-message',
25
- template: `
26
- <div #message>
27
- <ng-content></ng-content>
28
- </div>`
29
- }]
30
- }], propDecorators: { validatorName: [{
31
- type: Input,
32
- args: ['forValidator']
33
- }], message: [{
34
- type: ViewChild,
35
- args: ['message']
8
+ /**
9
+ * Component allows specifying custom validation message for the specified
10
+ * validator to override default message.
11
+ *
12
+ * Use this component as child in {@link NgxValidationMessagesComponent}.
13
+ */
14
+ class NgxCustomMessageComponent {
15
+ /**
16
+ * The name of the validator for which you want to override the message.
17
+ */
18
+ validatorName;
19
+ /**
20
+ * Contains overridden message for validator.
21
+ */
22
+ message;
23
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxCustomMessageComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
24
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: NgxCustomMessageComponent, selector: "ngx-custom-message", inputs: { validatorName: ["forValidator", "validatorName"] }, viewQueries: [{ propertyName: "message", first: true, predicate: ["message"], descendants: true }], ngImport: i0, template: `
25
+ <div #message>
26
+ <ng-content></ng-content>
27
+ </div>`, isInline: true });
28
+ }
29
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxCustomMessageComponent, decorators: [{
30
+ type: Component,
31
+ args: [{
32
+ selector: 'ngx-custom-message',
33
+ template: `
34
+ <div #message>
35
+ <ng-content></ng-content>
36
+ </div>`
37
+ }]
38
+ }], propDecorators: { validatorName: [{
39
+ type: Input,
40
+ args: ['forValidator']
41
+ }], message: [{
42
+ type: ViewChild,
43
+ args: ['message']
36
44
  }] } });
37
45
 
38
- /**
39
- * Allows to specify custom validator message to override default message.
40
- *
41
- * It's alternative for {@link NgxCustomMessageComponent}.
42
- * If you want to use standard html tags to define custom message, use this directive on html tag within
43
- * {@link NgxValidationMessagesComponent} specifying validator name as directive parameter,
44
- * otherwise use {@link NgxCustomMessageComponent}.
45
- */
46
- class NgxValidatorNameDirective {
47
- constructor(elem) {
48
- this.elem = elem;
49
- }
50
- ngAfterViewInit() {
51
- this.message = this.elem.nativeElement.innerText;
52
- }
53
- }
54
- NgxValidatorNameDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidatorNameDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
55
- NgxValidatorNameDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.1.1", type: NgxValidatorNameDirective, selector: "[ngxValidatorName]", inputs: { validatorName: "validatorName" }, ngImport: i0 });
56
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidatorNameDirective, decorators: [{
57
- type: Directive,
58
- args: [{
59
- selector: '[ngxValidatorName]'
60
- }]
61
- }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { validatorName: [{
62
- type: Input
46
+ /**
47
+ * Allows to specify custom validator message to override default message.
48
+ *
49
+ * It's alternative for {@link NgxCustomMessageComponent}.
50
+ * If you want to use standard html tags to define custom message, use this directive on html tag within
51
+ * {@link NgxValidationMessagesComponent} specifying validator name as directive parameter,
52
+ * otherwise use {@link NgxCustomMessageComponent}.
53
+ */
54
+ class NgxValidatorNameDirective {
55
+ elem;
56
+ /**
57
+ * The name of the validator for which you want to override the message.
58
+ */
59
+ validatorName;
60
+ /**
61
+ * Contains overridden message for validator.
62
+ */
63
+ message;
64
+ constructor(elem) {
65
+ this.elem = elem;
66
+ }
67
+ ngAfterViewInit() {
68
+ this.message = this.elem.nativeElement.innerText;
69
+ }
70
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidatorNameDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
71
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: NgxValidatorNameDirective, selector: "[ngxValidatorName]", inputs: { validatorName: "validatorName" }, ngImport: i0 });
72
+ }
73
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidatorNameDirective, decorators: [{
74
+ type: Directive,
75
+ args: [{
76
+ selector: '[ngxValidatorName]'
77
+ }]
78
+ }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { validatorName: [{
79
+ type: Input
63
80
  }] } });
64
81
 
65
82
  const NGX_VALIDATION_MESSAGES_CONFIG = new InjectionToken('NgxValidationMessagesConfig');
66
83
 
67
- /**
68
- * Service allows getting validation messages from client's settings.
69
- * Injecting {@link NgxValidationMessagesConfig} to get client's configuration with validation messages.
70
- */
71
- class NgxValidationMessagesService {
72
- constructor(messagesConfig) {
73
- this.messagesConfig = messagesConfig;
74
- /**
75
- * Regular expression to find params placeholder '#[paramName]'.
76
- */
77
- this.paramsRegExp = new RegExp(/#[[a-zA-Z_\\-]*]/);
78
- }
79
- /**
80
- * Get validation message for specified validator.
81
- *
82
- * @param validatorName for which to get message
83
- * @param params passed from validator for more detailed validation message
84
- *
85
- * @returns string validation message
86
- */
87
- getValidatorErrorMessages(validatorName, params) {
88
- const validationMessages = this.messagesConfig.messages;
89
- const validationMessage = validationMessages[validatorName];
90
- if (!validationMessage) {
91
- throw new Error('Validation message for validator: ' + validatorName
92
- + ' cannot be found, please check validation message key for validator it is case sensitive.');
93
- }
94
- if (params) {
95
- return this.expandParameterizedTemplateMessage(validationMessage, params);
96
- }
97
- return validationMessage;
98
- }
99
- expandParameterizedTemplateMessage(msg, params) {
100
- while (this.paramsRegExp.test(msg)) {
101
- const foundParams = this.paramsRegExp.exec(msg);
102
- foundParams.forEach(value => {
103
- const paramPlaceholder = value;
104
- value = value.replace('#[', '').replace(']', '');
105
- msg = msg.replace(paramPlaceholder, this.getParameter(params, value));
106
- });
107
- }
108
- return msg;
109
- }
110
- /**
111
- * A utilitarian method to get a property from an object.
112
- * if there is no property, an empty string is returned.
113
- *
114
- * @param obj from which the property must be obtained
115
- * @param prop property name
116
- *
117
- * @return property value or empty string
118
- */
119
- getParameter(obj, prop) {
120
- return prop.split('.')
121
- .reduce((m, i) => {
122
- return m && typeof m === 'object' ? m[i] : '';
123
- }, obj);
124
- }
125
- }
126
- NgxValidationMessagesService.SERVER_ERRORS = 'serverErrors';
127
- NgxValidationMessagesService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesService, deps: [{ token: NGX_VALIDATION_MESSAGES_CONFIG }], target: i0.ɵɵFactoryTarget.Injectable });
128
- NgxValidationMessagesService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesService });
129
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesService, decorators: [{
130
- type: Injectable
131
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
132
- type: Inject,
133
- args: [NGX_VALIDATION_MESSAGES_CONFIG]
84
+ /**
85
+ * Service allows getting validation messages from client's settings.
86
+ * Injecting {@link NgxValidationMessagesConfig} to get client's configuration with validation messages.
87
+ */
88
+ class NgxValidationMessagesService {
89
+ messagesConfig;
90
+ static SERVER_ERRORS = 'serverErrors';
91
+ /**
92
+ * Regular expression to find params placeholder '#[paramName]'.
93
+ */
94
+ paramsRegExp = new RegExp(/#[[a-zA-Z_\\-]*]/);
95
+ constructor(messagesConfig) {
96
+ this.messagesConfig = messagesConfig;
97
+ }
98
+ /**
99
+ * Get validation message for specified validator.
100
+ *
101
+ * @param validatorName for which to get message
102
+ * @param params passed from validator for more detailed validation message
103
+ *
104
+ * @returns string validation message
105
+ */
106
+ getValidatorErrorMessages(validatorName, params) {
107
+ const validationMessages = this.messagesConfig.messages;
108
+ const validationMessage = validationMessages[validatorName];
109
+ if (!validationMessage) {
110
+ throw new Error('Validation message for validator: ' + validatorName
111
+ + ' cannot be found, please check validation message key for validator it is case sensitive.');
112
+ }
113
+ if (params) {
114
+ return this.expandParameterizedTemplateMessage(validationMessage, params);
115
+ }
116
+ return validationMessage;
117
+ }
118
+ expandParameterizedTemplateMessage(msg, params) {
119
+ while (this.paramsRegExp.test(msg)) {
120
+ const foundParams = this.paramsRegExp.exec(msg);
121
+ foundParams.forEach(value => {
122
+ const paramPlaceholder = value;
123
+ value = value.replace('#[', '').replace(']', '');
124
+ msg = msg.replace(paramPlaceholder, this.getParameter(params, value));
125
+ });
126
+ }
127
+ return msg;
128
+ }
129
+ /**
130
+ * A utilitarian method to get a property from an object.
131
+ * if there is no property, an empty string is returned.
132
+ *
133
+ * @param obj from which the property must be obtained
134
+ * @param prop property name
135
+ *
136
+ * @return property value or empty string
137
+ */
138
+ getParameter(obj, prop) {
139
+ return prop.split('.')
140
+ .reduce((m, i) => {
141
+ return m && typeof m === 'object' ? m[i] : '';
142
+ }, obj);
143
+ }
144
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesService, deps: [{ token: NGX_VALIDATION_MESSAGES_CONFIG }], target: i0.ɵɵFactoryTarget.Injectable });
145
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesService });
146
+ }
147
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesService, decorators: [{
148
+ type: Injectable
149
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
150
+ type: Inject,
151
+ args: [NGX_VALIDATION_MESSAGES_CONFIG]
134
152
  }] }]; } });
135
153
 
136
- /**
137
- * Component for displaying validation messages, supports child components of type {@link NgxCustomMessageComponent}
138
- * and html elements with directive {@link NgxValidatorNameDirective} to override validation messages.
139
- */
140
- class NgxValidationMessagesComponent {
141
- constructor(ngxValidationConfig, ngxValidationMessagesService, directiveElementRef) {
142
- this.ngxValidationConfig = ngxValidationConfig;
143
- this.ngxValidationMessagesService = ngxValidationMessagesService;
144
- this.directiveElementRef = directiveElementRef;
145
- this.isMaterialError = false;
146
- /**
147
- * Key to get default validation message.
148
- */
149
- this.defaultError = 'error';
150
- this.materialErrorElement = 'MAT-ERROR';
151
- }
152
- ngAfterViewInit() {
153
- if (this.directiveElementRef && this.directiveElementRef.nativeElement
154
- && this.directiveElementRef.nativeElement.nodeName) {
155
- this.isMaterialError = this.materialErrorElement.toLocaleUpperCase()
156
- === this.directiveElementRef.nativeElement.nodeName.toLocaleUpperCase();
157
- }
158
- }
159
- /**
160
- * Get all validation messages for specified form control.
161
- */
162
- get errorMessages() {
163
- const result = [];
164
- if (!this.formControl || !this.formControl.errors) {
165
- return result;
166
- }
167
- if (this.customMsgComponent && this.customMsgComponent.length > 0) {
168
- this.processingCustomMessages(this.customMsgComponent);
169
- }
170
- if (this.customMsgDirective && this.customMsgDirective.length > 0) {
171
- this.processingCustomMessages(this.customMsgDirective);
172
- }
173
- for (const property in this.formControl.errors) {
174
- if (this.formControl.errors.hasOwnProperty(property)
175
- && (this.formControl.touched || this.formControl.dirty)) {
176
- if (this.formControl.errors[property].customMessages instanceof Array) {
177
- result.push(...this.formControl.errors[property].customMessages);
178
- continue;
179
- }
180
- if (this.formControl.errors[property].customMessage) {
181
- result.push(this.formControl.errors[property].customMessage);
182
- continue;
183
- }
184
- const validationMessage = this.ngxValidationMessagesService.getValidatorErrorMessages(property, this.formControl.errors[property]);
185
- result.push(validationMessage || this.formControl.errors[property].message
186
- || this.ngxValidationMessagesService.getValidatorErrorMessages(this.defaultError));
187
- }
188
- }
189
- return result;
190
- }
191
- processingCustomMessages(customMessage) {
192
- customMessage.forEach((msg) => {
193
- if (!this.formControl.errors[msg.validatorName]) {
194
- return;
195
- }
196
- let message = (msg instanceof NgxCustomMessageComponent)
197
- ? msg.message.nativeElement.innerText : msg.message;
198
- message = this.ngxValidationMessagesService.expandParameterizedTemplateMessage(message, this.formControl.errors[msg.validatorName]);
199
- typeof this.formControl.errors[msg.validatorName] === 'object'
200
- ? this.formControl.errors[msg.validatorName].customMessage = message
201
- : this.formControl.errors[msg.validatorName] = { customMessage: message };
202
- });
203
- }
204
- }
205
- NgxValidationMessagesComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesComponent, deps: [{ token: NGX_VALIDATION_MESSAGES_CONFIG }, { token: NgxValidationMessagesService }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
206
- NgxValidationMessagesComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "13.1.1", type: NgxValidationMessagesComponent, selector: "ngx-validation-messages, [ngxValidationMessages]", inputs: { formControl: ["for", "formControl"] }, queries: [{ propertyName: "customMsgComponent", predicate: NgxCustomMessageComponent }, { propertyName: "customMsgDirective", predicate: NgxValidatorNameDirective }], ngImport: i0, template: "<!--To show error messages standalone without component-->\r\n<div *ngIf=\"!formControl && customMsgComponent.length == 0 && customMsgDirective.length == 0\"\r\n [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\r\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\r\n <ng-content></ng-content>\r\n </span>\r\n</div>\r\n\r\n<ng-container *ngIf=\"errorMessages.length > 0\">\r\n\r\n <!--To show error messages in material ui style-->\r\n <ng-container *ngIf=\"isMaterialError; else withoutMaterial\">\r\n <ng-container *ngFor=\"let message of errorMessages\">\r\n <mat-error>{{ message }}</mat-error>\r\n </ng-container>\r\n </ng-container>\r\n\r\n <!--To show error messages in user style-->\r\n <ng-template #withoutMaterial>\r\n <div [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\r\n <div *ngFor=\"let message of errorMessages\">\r\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\r\n {{ message }}\r\n </span>\r\n </div>\r\n </div>\r\n </ng-template>\r\n\r\n</ng-container>\r\n", styles: [".ngx_vm__def_error_block{position:relative;display:inline-block;width:100%;margin-bottom:10px;margin-top:2px;padding:8px;border-radius:0;background:#c51244;box-shadow:1px 1px 1px #aaa;opacity:.8}.ngx_vm__def_error_text{color:#fff}\n"], directives: [{ type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i3.MatError, selector: "mat-error", inputs: ["id"] }] });
207
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesComponent, decorators: [{
208
- type: Component,
209
- args: [{ selector: 'ngx-validation-messages, [ngxValidationMessages]', template: "<!--To show error messages standalone without component-->\r\n<div *ngIf=\"!formControl && customMsgComponent.length == 0 && customMsgDirective.length == 0\"\r\n [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\r\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\r\n <ng-content></ng-content>\r\n </span>\r\n</div>\r\n\r\n<ng-container *ngIf=\"errorMessages.length > 0\">\r\n\r\n <!--To show error messages in material ui style-->\r\n <ng-container *ngIf=\"isMaterialError; else withoutMaterial\">\r\n <ng-container *ngFor=\"let message of errorMessages\">\r\n <mat-error>{{ message }}</mat-error>\r\n </ng-container>\r\n </ng-container>\r\n\r\n <!--To show error messages in user style-->\r\n <ng-template #withoutMaterial>\r\n <div [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\r\n <div *ngFor=\"let message of errorMessages\">\r\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\r\n {{ message }}\r\n </span>\r\n </div>\r\n </div>\r\n </ng-template>\r\n\r\n</ng-container>\r\n", styles: [".ngx_vm__def_error_block{position:relative;display:inline-block;width:100%;margin-bottom:10px;margin-top:2px;padding:8px;border-radius:0;background:#c51244;box-shadow:1px 1px 1px #aaa;opacity:.8}.ngx_vm__def_error_text{color:#fff}\n"] }]
210
- }], ctorParameters: function () { return [{ type: undefined, decorators: [{
211
- type: Inject,
212
- args: [NGX_VALIDATION_MESSAGES_CONFIG]
213
- }] }, { type: NgxValidationMessagesService }, { type: i0.ElementRef }]; }, propDecorators: { formControl: [{
214
- type: Input,
215
- args: ['for']
216
- }], customMsgComponent: [{
217
- type: ContentChildren,
218
- args: [NgxCustomMessageComponent]
219
- }], customMsgDirective: [{
220
- type: ContentChildren,
221
- args: [NgxValidatorNameDirective]
154
+ /**
155
+ * Component for displaying validation messages, supports child components of type {@link NgxCustomMessageComponent}
156
+ * and html elements with directive {@link NgxValidatorNameDirective} to override validation messages.
157
+ */
158
+ class NgxValidationMessagesComponent {
159
+ ngxValidationConfig;
160
+ ngxValidationMessagesService;
161
+ directiveElementRef;
162
+ /**
163
+ * Form control for which need to show validation messages.
164
+ */
165
+ formControl;
166
+ /**
167
+ * Contains {@link NgxCustomMessageComponent} if present.
168
+ */
169
+ customMsgComponent;
170
+ /**
171
+ * Contains {@link NgxValidatorNameDirective} if present.
172
+ */
173
+ customMsgDirective;
174
+ isMaterialError = false;
175
+ /**
176
+ * Key to get default validation message.
177
+ */
178
+ defaultError = 'error';
179
+ materialErrorElement = 'MAT-ERROR';
180
+ constructor(ngxValidationConfig, ngxValidationMessagesService, directiveElementRef) {
181
+ this.ngxValidationConfig = ngxValidationConfig;
182
+ this.ngxValidationMessagesService = ngxValidationMessagesService;
183
+ this.directiveElementRef = directiveElementRef;
184
+ }
185
+ ngAfterViewInit() {
186
+ if (this.directiveElementRef && this.directiveElementRef.nativeElement
187
+ && this.directiveElementRef.nativeElement.nodeName) {
188
+ this.isMaterialError = this.materialErrorElement.toLocaleUpperCase()
189
+ === this.directiveElementRef.nativeElement.nodeName.toLocaleUpperCase();
190
+ }
191
+ }
192
+ /**
193
+ * Get all validation messages for specified form control.
194
+ */
195
+ get errorMessages() {
196
+ const result = [];
197
+ if (!this.formControl || !this.formControl.errors) {
198
+ return result;
199
+ }
200
+ if (this.customMsgComponent && this.customMsgComponent.length > 0) {
201
+ this.processingCustomMessages(this.customMsgComponent);
202
+ }
203
+ if (this.customMsgDirective && this.customMsgDirective.length > 0) {
204
+ this.processingCustomMessages(this.customMsgDirective);
205
+ }
206
+ for (const property in this.formControl.errors) {
207
+ if (this.formControl.errors.hasOwnProperty(property)
208
+ && (this.formControl.touched || this.formControl.dirty)) {
209
+ if (this.formControl.errors[property].customMessages instanceof Array) {
210
+ result.push(...this.formControl.errors[property].customMessages);
211
+ continue;
212
+ }
213
+ if (this.formControl.errors[property].customMessage) {
214
+ result.push(this.formControl.errors[property].customMessage);
215
+ continue;
216
+ }
217
+ const validationMessage = this.ngxValidationMessagesService.getValidatorErrorMessages(property, this.formControl.errors[property]);
218
+ result.push(validationMessage || this.formControl.errors[property].message
219
+ || this.ngxValidationMessagesService.getValidatorErrorMessages(this.defaultError));
220
+ }
221
+ }
222
+ return result;
223
+ }
224
+ processingCustomMessages(customMessage) {
225
+ customMessage.forEach((msg) => {
226
+ if (!this.formControl.errors[msg.validatorName]) {
227
+ return;
228
+ }
229
+ let message = (msg instanceof NgxCustomMessageComponent)
230
+ ? msg.message.nativeElement.innerText : msg.message;
231
+ message = this.ngxValidationMessagesService.expandParameterizedTemplateMessage(message, this.formControl.errors[msg.validatorName]);
232
+ typeof this.formControl.errors[msg.validatorName] === 'object'
233
+ ? this.formControl.errors[msg.validatorName].customMessage = message
234
+ : this.formControl.errors[msg.validatorName] = { customMessage: message };
235
+ });
236
+ }
237
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesComponent, deps: [{ token: NGX_VALIDATION_MESSAGES_CONFIG }, { token: NgxValidationMessagesService }, { token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Component });
238
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: NgxValidationMessagesComponent, selector: "ngx-validation-messages, [ngxValidationMessages]", inputs: { formControl: ["for", "formControl"] }, queries: [{ propertyName: "customMsgComponent", predicate: NgxCustomMessageComponent }, { propertyName: "customMsgDirective", predicate: NgxValidatorNameDirective }], ngImport: i0, template: "<!--To show error messages standalone without component-->\n<div *ngIf=\"!formControl && customMsgComponent.length == 0 && customMsgDirective.length == 0\"\n [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\n <ng-content></ng-content>\n </span>\n</div>\n\n<ng-container *ngIf=\"errorMessages.length > 0\">\n\n <!--To show error messages in material ui style-->\n <ng-container *ngIf=\"isMaterialError; else withoutMaterial\">\n <ng-container *ngFor=\"let message of errorMessages\">\n <mat-error>{{ message }}</mat-error>\n </ng-container>\n </ng-container>\n\n <!--To show error messages in user style-->\n <ng-template #withoutMaterial>\n <div [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\n <div *ngFor=\"let message of errorMessages\">\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\n {{ message }}\n </span>\n </div>\n </div>\n </ng-template>\n\n</ng-container>\n", styles: [".ngx_vm__def_error_block{position:relative;display:inline-block;width:100%;margin-bottom:10px;margin-top:2px;padding:8px;border-radius:0;background:#c51244;box-shadow:1px 1px 1px #aaa;opacity:.8}.ngx_vm__def_error_text{color:#fff}\n"], dependencies: [{ kind: "directive", type: i2.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.MatError, selector: "mat-error, [matError]", inputs: ["id"] }] });
239
+ }
240
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesComponent, decorators: [{
241
+ type: Component,
242
+ args: [{ selector: 'ngx-validation-messages, [ngxValidationMessages]', template: "<!--To show error messages standalone without component-->\n<div *ngIf=\"!formControl && customMsgComponent.length == 0 && customMsgDirective.length == 0\"\n [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\n <ng-content></ng-content>\n </span>\n</div>\n\n<ng-container *ngIf=\"errorMessages.length > 0\">\n\n <!--To show error messages in material ui style-->\n <ng-container *ngIf=\"isMaterialError; else withoutMaterial\">\n <ng-container *ngFor=\"let message of errorMessages\">\n <mat-error>{{ message }}</mat-error>\n </ng-container>\n </ng-container>\n\n <!--To show error messages in user style-->\n <ng-template #withoutMaterial>\n <div [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.blockClassNames || 'ngx_vm__def_error_block'\">\n <div *ngFor=\"let message of errorMessages\">\n <span [ngClass]=\"ngxValidationConfig?.validationMessagesStyle?.textClassNames || 'ngx_vm__def_error_text'\">\n {{ message }}\n </span>\n </div>\n </div>\n </ng-template>\n\n</ng-container>\n", styles: [".ngx_vm__def_error_block{position:relative;display:inline-block;width:100%;margin-bottom:10px;margin-top:2px;padding:8px;border-radius:0;background:#c51244;box-shadow:1px 1px 1px #aaa;opacity:.8}.ngx_vm__def_error_text{color:#fff}\n"] }]
243
+ }], ctorParameters: function () { return [{ type: undefined, decorators: [{
244
+ type: Inject,
245
+ args: [NGX_VALIDATION_MESSAGES_CONFIG]
246
+ }] }, { type: NgxValidationMessagesService }, { type: i0.ElementRef }]; }, propDecorators: { formControl: [{
247
+ type: Input,
248
+ args: ['for']
249
+ }], customMsgComponent: [{
250
+ type: ContentChildren,
251
+ args: [NgxCustomMessageComponent]
252
+ }], customMsgDirective: [{
253
+ type: ContentChildren,
254
+ args: [NgxValidatorNameDirective]
222
255
  }] } });
223
256
 
224
- class NgxValidationMessagesModule {
225
- static forRoot(config) {
226
- return {
227
- ngModule: NgxValidationMessagesModule,
228
- providers: [
229
- NgxValidationMessagesService,
230
- {
231
- provide: NGX_VALIDATION_MESSAGES_CONFIG,
232
- useValue: config
233
- }
234
- ]
235
- };
236
- }
237
- }
238
- NgxValidationMessagesModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
239
- NgxValidationMessagesModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesModule, declarations: [NgxValidationMessagesComponent,
240
- NgxCustomMessageComponent,
241
- NgxValidatorNameDirective], imports: [CommonModule,
242
- MatFormFieldModule], exports: [NgxValidationMessagesComponent,
243
- NgxCustomMessageComponent,
244
- NgxValidatorNameDirective] });
245
- NgxValidationMessagesModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesModule, imports: [[
246
- CommonModule,
247
- MatFormFieldModule
248
- ]] });
249
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.1.1", ngImport: i0, type: NgxValidationMessagesModule, decorators: [{
250
- type: NgModule,
251
- args: [{
252
- imports: [
253
- CommonModule,
254
- MatFormFieldModule
255
- ],
256
- declarations: [
257
- NgxValidationMessagesComponent,
258
- NgxCustomMessageComponent,
259
- NgxValidatorNameDirective
260
- ],
261
- exports: [
262
- NgxValidationMessagesComponent,
263
- NgxCustomMessageComponent,
264
- NgxValidatorNameDirective
265
- ]
266
- }]
257
+ class NgxValidationMessagesModule {
258
+ static forRoot(config) {
259
+ return {
260
+ ngModule: NgxValidationMessagesModule,
261
+ providers: [
262
+ NgxValidationMessagesService,
263
+ {
264
+ provide: NGX_VALIDATION_MESSAGES_CONFIG,
265
+ useValue: config
266
+ }
267
+ ]
268
+ };
269
+ }
270
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
271
+ static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesModule, declarations: [NgxValidationMessagesComponent,
272
+ NgxCustomMessageComponent,
273
+ NgxValidatorNameDirective], imports: [CommonModule,
274
+ MatFormFieldModule], exports: [NgxValidationMessagesComponent,
275
+ NgxCustomMessageComponent,
276
+ NgxValidatorNameDirective] });
277
+ static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesModule, imports: [CommonModule,
278
+ MatFormFieldModule] });
279
+ }
280
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: NgxValidationMessagesModule, decorators: [{
281
+ type: NgModule,
282
+ args: [{
283
+ imports: [
284
+ CommonModule,
285
+ MatFormFieldModule,
286
+ ],
287
+ declarations: [
288
+ NgxValidationMessagesComponent,
289
+ NgxCustomMessageComponent,
290
+ NgxValidatorNameDirective
291
+ ],
292
+ exports: [
293
+ NgxValidationMessagesComponent,
294
+ NgxCustomMessageComponent,
295
+ NgxValidatorNameDirective
296
+ ]
297
+ }]
267
298
  }] });
268
299
 
269
- /*
270
- * Public API Surface of ngx-validation-messages
300
+ /*
301
+ * Public API Surface of ngx-validation-messages
271
302
  */
272
303
 
273
- /**
274
- * Generated bundle index. Do not edit.
304
+ /**
305
+ * Generated bundle index. Do not edit.
275
306
  */
276
307
 
277
308
  export { NGX_VALIDATION_MESSAGES_CONFIG, NgxCustomMessageComponent, NgxValidationMessagesComponent, NgxValidationMessagesModule, NgxValidatorNameDirective };