@lagoshny/ngx-validation-messages 1.1.1 → 2.0.2
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.
- package/README.md +425 -363
- package/bundles/lagoshny-ngx-validation-messages.umd.js +563 -549
- package/bundles/lagoshny-ngx-validation-messages.umd.js.map +1 -1
- package/esm2015/lagoshny-ngx-validation-messages.js +5 -5
- package/esm2015/lib/components/ngx-custom-message/ngx-custom-message.component.js +23 -23
- package/esm2015/lib/components/ngx-validation-messages/ngx-validation-messages.component.js +92 -92
- package/esm2015/lib/directivies/ngx-validator-name.directive.js +29 -29
- package/esm2015/lib/interface/ngx-validation-messages.config.js +3 -3
- package/esm2015/lib/ngx-validation-messages.module.js +45 -48
- package/esm2015/lib/service/ngx-validation-messages.service.js +69 -69
- package/esm2015/public-api.js +5 -5
- package/fesm2015/lagoshny-ngx-validation-messages.js +231 -234
- package/fesm2015/lagoshny-ngx-validation-messages.js.map +1 -1
- package/lagoshny-ngx-validation-messages.d.ts +5 -5
- package/lagoshny-ngx-validation-messages.metadata.json +1 -1
- package/lib/components/ngx-custom-message/ngx-custom-message.component.d.ts +17 -17
- package/lib/components/ngx-validation-messages/ngx-validation-messages.component.d.ts +37 -39
- package/lib/directivies/ngx-validator-name.directive.d.ts +22 -22
- package/lib/interface/ngx-validation-messages.config.d.ts +17 -17
- package/lib/ngx-validation-messages.module.d.ts +9 -9
- package/lib/service/ngx-validation-messages.service.d.ts +34 -34
- package/package.json +7 -6
- package/public-api.d.ts +1 -1
- package/bundles/lagoshny-ngx-validation-messages.umd.min.js +0 -2
- package/bundles/lagoshny-ngx-validation-messages.umd.min.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,363 +1,425 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
]
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
<
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
````
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
1
|
+
# NgxValidationMessages
|
|
2
|
+
<a href="https://www.npmjs.com/package/@lagoshny/ngx-validation-messages/v/lts-view-engine">
|
|
3
|
+
<img src="https://img.shields.io/npm/v/@lagoshny/ngx-validation-messages/lts-view-engine?label=npm" alt="LTS view engine npm version" />
|
|
4
|
+
</a>
|
|
5
|
+
|
|
6
|
+
<a href="https://github.com/lagoshny/ngx-validation-messages/actions?query=workflow%3ABuild">
|
|
7
|
+
<img src="https://img.shields.io/github/workflow/status/lagoshny/ngx-validation-messages/Build/lts-view-engine" alt="Pipeline info" />
|
|
8
|
+
</a>
|
|
9
|
+
|
|
10
|
+
<a href="https://github.com/lagoshny/ngx-validation-messages/issues">
|
|
11
|
+
<img src="https://img.shields.io/github/issues/lagoshny/ngx-validation-messages" alt="Total open issues" />
|
|
12
|
+
</a>
|
|
13
|
+
|
|
14
|
+
<a href="https://www.npmjs.com/package/@lagoshny/ngx-validation-messages">
|
|
15
|
+
<img src="https://img.shields.io/npm/dt/@lagoshny/ngx-validation-messages" alt="Total downloads by npm" />
|
|
16
|
+
</a>
|
|
17
|
+
|
|
18
|
+
<a href="https://mit-license.org/">
|
|
19
|
+
<img src="https://img.shields.io/npm/l/@lagoshny/ngx-validation-messages" alt="License info" />
|
|
20
|
+
</a>
|
|
21
|
+
|
|
22
|
+
<br />
|
|
23
|
+
<br />
|
|
24
|
+
|
|
25
|
+
## ⚠ Lib version that compatible with Angular 6-12 versions that uses old `View Engine` compilation.
|
|
26
|
+
|
|
27
|
+
### 💥 If you use new `Ivy` compilation or Angular 13 you should use the [latest](https://github.com/lagoshny/ngx-validation-messages) lib version.
|
|
28
|
+
>
|
|
29
|
+
>See more about it [here](https://github.com/lagoshny/ngx-validation-messages/blob/master/CHANGELOG.md#300-2021-12-23).
|
|
30
|
+
|
|
31
|
+
### New versioning policy.
|
|
32
|
+
|
|
33
|
+
- Versions that work with old `View Engine` compilation [`2.0.0`-`2.x.x`].
|
|
34
|
+
|
|
35
|
+
- Versions that work with new `Ivy` compilation [`3.0.0`-`x.x.x`].
|
|
36
|
+
|
|
37
|
+
This library allows you to decrease boilerplate code when handling validations error messages.
|
|
38
|
+
## Contents
|
|
39
|
+
1. [Changelog](#Changelog)
|
|
40
|
+
1. [Getting started](#Getting-started)
|
|
41
|
+
1. [Installation](#Installation)
|
|
42
|
+
2. [Base configuration](#Base-configuration)
|
|
43
|
+
2. [Usage](#Usage)
|
|
44
|
+
1. [Template driven approach (ngModel)](#1-template-driven-approach-ngmodel)
|
|
45
|
+
2. [Form driven approach (reactive)](#2-form-driven-approach-reactive)
|
|
46
|
+
3. [Without component as error container](#3-use-without-component-as-error-container)
|
|
47
|
+
4. [With material ui components using mat-error component](#4-with-material-ui-components-using-mat-error-component)
|
|
48
|
+
3. [How it works?](#How-it-works?)
|
|
49
|
+
4. [Advanced configuration](#Advanced-configuration)
|
|
50
|
+
1. [Override configured validation messages](#Override-configured-validation-messages)
|
|
51
|
+
2. [Custom display validation messages styles](#Custom-display-validation-messages-styles)
|
|
52
|
+
5. [Writing custom validators](#Writing-custom-validators)
|
|
53
|
+
1. [Example custom validator](#Example-custom-validator)
|
|
54
|
+
6. [Further improvements](#Further-improvements)
|
|
55
|
+
|
|
56
|
+
## Changelog
|
|
57
|
+
[Learn about the latest improvements](https://github.com/lagoshny/ngx-validation-messages/blob/lts-view-engine/CHANGELOG.md).
|
|
58
|
+
|
|
59
|
+
## Getting started
|
|
60
|
+
|
|
61
|
+
### Installation
|
|
62
|
+
```
|
|
63
|
+
npm install @lagoshny/ngx-validation-messages@lts-view-engine --save
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Base configuration
|
|
67
|
+
|
|
68
|
+
To work with main `NgxValidationMessagesComponent` which decrease boilerplate validation code you need
|
|
69
|
+
in the root application module import `NgxValidationMessagesModule` passing configuration which contains validation messages for validators:
|
|
70
|
+
```typescript
|
|
71
|
+
// ...
|
|
72
|
+
import { NgxValidationMessagesModule } from '@lagoshny/ngx-validation-messages';
|
|
73
|
+
|
|
74
|
+
@NgModule({
|
|
75
|
+
imports: [
|
|
76
|
+
NgxValidationMessagesModule.forRoot({
|
|
77
|
+
messages: {
|
|
78
|
+
// Key is validator name, value is validator message
|
|
79
|
+
required: 'This is required filed!',
|
|
80
|
+
// If validator gets params, you can specify params placeholder in the validation message
|
|
81
|
+
// to get validator params values for constructing more detail message
|
|
82
|
+
maxlength: 'Max count symbols are #[requiredLength]',
|
|
83
|
+
minlength: 'Min count symbols are #[requiredLength]'
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
]
|
|
87
|
+
})
|
|
88
|
+
export class AppRootModule {
|
|
89
|
+
}
|
|
90
|
+
// ...
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
##### Note: validator's name specified in configuration case sensitive, if you will use name 'maxLength' instead 'maxlength' your message will not apply. In `console output` you will see error about it.
|
|
94
|
+
|
|
95
|
+
In other modules where you want to use `NgxValidationMessagesComponent`, you need simple import
|
|
96
|
+
`NgxValidationMessagesModule`:
|
|
97
|
+
```typescript
|
|
98
|
+
// ...
|
|
99
|
+
import { NgxValidationMessagesModule } from '@lagoshny/ngx-validation-messages';
|
|
100
|
+
|
|
101
|
+
@NgModule({
|
|
102
|
+
imports: [
|
|
103
|
+
NgxValidationMessagesModule
|
|
104
|
+
]
|
|
105
|
+
})
|
|
106
|
+
export class SomeModule {
|
|
107
|
+
}
|
|
108
|
+
// ...
|
|
109
|
+
```
|
|
110
|
+
After that you can use the `NgxValidationMessagesComponent` in your templates, about it see below.
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
### Usage
|
|
114
|
+
#### 1. Template driven approach (ngModel)
|
|
115
|
+
Usually, when you need to show validation message to a user you need to do in your component's template something like this:
|
|
116
|
+
````html
|
|
117
|
+
<input type="text"
|
|
118
|
+
required
|
|
119
|
+
minlength="3"
|
|
120
|
+
[(ngModel)]="user.firstName"
|
|
121
|
+
name="firstName"
|
|
122
|
+
#firstNameVar="ngModel"/>
|
|
123
|
+
<span class="error-block" *ngIf="(firstNameVar.touched || firstNameVar.dirty) && firstNameVar.errors">
|
|
124
|
+
<span *ngIf="firstNameVar.hasError('required')">
|
|
125
|
+
Please enter your first name.
|
|
126
|
+
</span>
|
|
127
|
+
<span *ngIf="firstNameVar.hasError('minlength')">
|
|
128
|
+
The first name must be longer than 3 characters.
|
|
129
|
+
</span>
|
|
130
|
+
</span>
|
|
131
|
+
````
|
|
132
|
+
And if you add new one validator to firstName input also you need to add new <span> blocks with validation message.
|
|
133
|
+
|
|
134
|
+
Instead of this boilerplate code you can add `<ngx-validation-messages>` component to your HTML markup and reduce the number of trash code:
|
|
135
|
+
|
|
136
|
+
```html
|
|
137
|
+
<input type="text"
|
|
138
|
+
required
|
|
139
|
+
minlength="3"
|
|
140
|
+
[(ngModel)]="user.firstName"
|
|
141
|
+
name="firstName"
|
|
142
|
+
#firstNameVar="n~~**~~gModel"/>
|
|
143
|
+
<n~~**~~gx-validation-messages [for]='firstNameVar'></ngx-validation-messages>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### 2. Form driven approach (reactive)
|
|
147
|
+
|
|
148
|
+
In reactive approach you define a component class which building form controls and apply control validators:
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
@Component(
|
|
152
|
+
// ...
|
|
153
|
+
)
|
|
154
|
+
export class UserFormComponent {
|
|
155
|
+
|
|
156
|
+
public userForm: FormGroup;
|
|
157
|
+
|
|
158
|
+
constructor(private formBuilder: FormBuilder) {
|
|
159
|
+
this.userForm = this.formBuilder.group({
|
|
160
|
+
firstName: ['', [Validators.required, Validators.minLength(3)]]
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
For defined above component class you will have a HTML template like this:
|
|
168
|
+
|
|
169
|
+
````html
|
|
170
|
+
<form [formGroup]="userForm">
|
|
171
|
+
...
|
|
172
|
+
<input type="text"
|
|
173
|
+
required
|
|
174
|
+
minlength="3"
|
|
175
|
+
formControlName="firstName"/>
|
|
176
|
+
<span class="error-block" *ngIf="(userForm.get('firstName').touched
|
|
177
|
+
|| userForm.get('firstName').dirty) && userForm.get('firstName').errors">
|
|
178
|
+
<span *ngIf="userForm.get('firstName').hasError('required')">
|
|
179
|
+
Please enter your first name.
|
|
180
|
+
</span>
|
|
181
|
+
<span *ngIf="userForm.get('firstName').hasError('minlength')">
|
|
182
|
+
The first name must be longer than 3 characters.
|
|
183
|
+
</span>
|
|
184
|
+
</span>
|
|
185
|
+
...
|
|
186
|
+
</form>
|
|
187
|
+
````
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
As you can see there is again a lot of boilerplate code.
|
|
191
|
+
|
|
192
|
+
You can decrease it using `NgxValidationMessagesComponent`:
|
|
193
|
+
|
|
194
|
+
````html
|
|
195
|
+
<form [formGroup]="userForm">
|
|
196
|
+
...
|
|
197
|
+
<input type="text"
|
|
198
|
+
required
|
|
199
|
+
minlength="3"
|
|
200
|
+
formControlName="firstName"/>
|
|
201
|
+
<ngx-validation-messages [for]="taskForm.get('firstName')"></ngx-validation-messages>
|
|
202
|
+
...
|
|
203
|
+
</form>
|
|
204
|
+
````
|
|
205
|
+
|
|
206
|
+
#### 3. Use without component as error container
|
|
207
|
+
If you need simple to display error message in common style as it does `ngx-validation-messages` component you can do it as below:
|
|
208
|
+
|
|
209
|
+
````html
|
|
210
|
+
<form >
|
|
211
|
+
...
|
|
212
|
+
<ngx-validation-messages *ngIf="showError">
|
|
213
|
+
Your error message
|
|
214
|
+
</ngx-validation-messages>
|
|
215
|
+
...
|
|
216
|
+
</form>
|
|
217
|
+
````
|
|
218
|
+
|
|
219
|
+
#### 4. With material ui components using mat-error component
|
|
220
|
+
It's simple to use `ngx-validation-messages` with material ui `mat-error` component, to do this you need to put `ngxValidationMessages` directive to `mat-error` component like this:
|
|
221
|
+
|
|
222
|
+
````html
|
|
223
|
+
<form [formGroup]="taskForm">
|
|
224
|
+
....
|
|
225
|
+
<mat-form-field>
|
|
226
|
+
<input matInput
|
|
227
|
+
formControlName="email">
|
|
228
|
+
<mat-error ngxValidationMessages [for]="taskForm.get('email')"></mat-error>
|
|
229
|
+
</mat-form-field>
|
|
230
|
+
...
|
|
231
|
+
</form>
|
|
232
|
+
````
|
|
233
|
+
|
|
234
|
+
After that if `FormControl` with name `email` will be invalid, then configured error messages for validators applied to `email` will be shown in material ui style.
|
|
235
|
+
|
|
236
|
+
Also you can override configured error messages for concrete case in standard way:
|
|
237
|
+
|
|
238
|
+
````html
|
|
239
|
+
<mat-form-field>
|
|
240
|
+
<input matInput
|
|
241
|
+
formControlName="email">
|
|
242
|
+
<mat-error ngxValidationMessages [for]="taskForm.get('email')">
|
|
243
|
+
<ngx-custom-message forValidator="required">Your new message</ngx-custom-message>
|
|
244
|
+
</mat-error>
|
|
245
|
+
</mat-form-field>
|
|
246
|
+
````
|
|
247
|
+
|
|
248
|
+
## How it works?
|
|
249
|
+
|
|
250
|
+
In both cases `NgxValidationMessagesComponent` will get validation messages for each applied to form control validator from passed configuration object to `NgxValidationMessagesModule`
|
|
251
|
+
|
|
252
|
+
For example, if you pass configuration like this:
|
|
253
|
+
|
|
254
|
+
````typescript
|
|
255
|
+
//...
|
|
256
|
+
|
|
257
|
+
NgxValidationMessagesModule.forRoot({
|
|
258
|
+
messages: {
|
|
259
|
+
required: 'This is required filed!',
|
|
260
|
+
minlength: 'Min count symbols are #[requiredLength]'
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
//...
|
|
265
|
+
|
|
266
|
+
````
|
|
267
|
+
`NgxValidationMessagesComponent` will get ***This is required filed!*** message for **required** validator
|
|
268
|
+
and ***Min count symbols are #[requiredLength]*** message with parameter placeholder for **minlength** validator.
|
|
269
|
+
|
|
270
|
+
- **#[requiredLength]** in the example above is param placeholder value It means this placeholder will replace to real param passed to **minlength** validator.
|
|
271
|
+
|
|
272
|
+
##### Note: You need to pass correct names for param placeholder, otherwise you will get `undefined` value instead param value.
|
|
273
|
+
|
|
274
|
+
To get right param names you need to check what params returns concrete validator when a value is not valid.
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
For example **maxlength or minlength** standard validators return passed length param in validation result using **requiredLength** name and we can use its name with param placeholder in a validation message.
|
|
278
|
+
|
|
279
|
+
## Advanced configuration
|
|
280
|
+
### Override configured validation messages
|
|
281
|
+
|
|
282
|
+
If you want to specify a different message for some validator in the concrete HTML template you can use one of the following ways to override configured validation messages passed to `NgxValidationMessagesModule`.
|
|
283
|
+
|
|
284
|
+
#### 1. Use `NgxCustomMessageComponent` to override validation message with custom HTML-tag component
|
|
285
|
+
|
|
286
|
+
To override validation message for some validator, you can use `<ngx-custom-message></ngx-custom-message>` component as child for
|
|
287
|
+
`<ngx-validation-messages></ngx-validation-messages>`:
|
|
288
|
+
|
|
289
|
+
```html
|
|
290
|
+
...
|
|
291
|
+
<input type="text"
|
|
292
|
+
required
|
|
293
|
+
minlength="3"
|
|
294
|
+
[(ngModel)]="user.firstName"
|
|
295
|
+
name="firstName"
|
|
296
|
+
#firstNameVar="ngModel"/>
|
|
297
|
+
<ngx-validation-messages [for]='firstNameVar'>
|
|
298
|
+
<!-- Param forValidator accepts validator name to override message -->
|
|
299
|
+
<ngx-custom-message forValidator='minlength'>
|
|
300
|
+
Min length for first name is #[requiredLength]
|
|
301
|
+
</ngx-custom-message>
|
|
302
|
+
</ngx-validation-messages>
|
|
303
|
+
...
|
|
304
|
+
```
|
|
305
|
+
In this case, for **required** validator will be used configured ***This is required filed!*** message, but for
|
|
306
|
+
**minlength** validator will be used overridden ***Min length for first name is #[requiredLength]*** message
|
|
307
|
+
instead of defined in the configuration ***Min count symbols are #[requiredLength]*** .
|
|
308
|
+
|
|
309
|
+
##### Note: we can also use params placeholder in redefined a validation message in the same way as in the configuration object.
|
|
310
|
+
|
|
311
|
+
#### 2. Use `NgxValidatorNameDirective` to override validation message with standard HTML-tag component
|
|
312
|
+
|
|
313
|
+
The second way to override message is using directive applied to child `<ngx-validation-messages></ngx-validation-messages>` HTML tag:
|
|
314
|
+
|
|
315
|
+
```html
|
|
316
|
+
...
|
|
317
|
+
<input type="text"
|
|
318
|
+
required
|
|
319
|
+
minlength="3"
|
|
320
|
+
[(ngModel)]="user.firstName"
|
|
321
|
+
name="firstName"
|
|
322
|
+
#firstNameVar="ngModel"/>
|
|
323
|
+
<ngx-validation-messages [for]='firstNameVar'>
|
|
324
|
+
<!-- Directive parameter accepts a validator name to override message -->
|
|
325
|
+
<span ngxValidatorName='minlength'>
|
|
326
|
+
Min length for first name is #[requiredLength]
|
|
327
|
+
</span>
|
|
328
|
+
</ngx-validation-messages>
|
|
329
|
+
...
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Custom display validation messages styles
|
|
333
|
+
|
|
334
|
+
If you want to change display default validation message styles, you can set custom CSS classes in the passed configuration for `NgxValidationMessagesModule` use optional param ***validationMessagesStyle***:
|
|
335
|
+
```typescript
|
|
336
|
+
//...
|
|
337
|
+
|
|
338
|
+
NgxValidationMessagesModule.forRoot({
|
|
339
|
+
messages: {
|
|
340
|
+
required: 'This is required filed!',
|
|
341
|
+
minlength: 'Min count symbols are #[requiredLength]'
|
|
342
|
+
},
|
|
343
|
+
validationMessagesStyle: {
|
|
344
|
+
// Styles for changing the view of a validation message block
|
|
345
|
+
blockClassNames: 'your_block_css_class1 your_block_css_class2',
|
|
346
|
+
// Styles for changing the view of validation message text inside message block
|
|
347
|
+
textClassNames: 'your_text_css_class1 your_text_css_class2'
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
//...
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Writing custom validators
|
|
355
|
+
|
|
356
|
+
If you want to write custom validator then for working `NgxValidationMessagesComponent` you need to follow some rule: **returned validation result should be in the following formats**:
|
|
357
|
+
|
|
358
|
+
- if validator **with/without** parameters and validation was **success** then validator should return `null` or `undefined`
|
|
359
|
+
|
|
360
|
+
- if validator **with** parameters and validation was **not success** then validator should return an object which contains `validator name` as key and `passed to validator params` as value.
|
|
361
|
+
For example for `range` validator you will return `{ range: {min, max} }` where `min` and `max` passed to validator params
|
|
362
|
+
These params you can get in validation message using params placeholder.
|
|
363
|
+
|
|
364
|
+
- if validator **without** parameters and validation was **not success** then validator should return an object which contains `validator name` as key and `true` as value.
|
|
365
|
+
For example for `passwordMatch` validator you will return `{ passwordMatch: true }`
|
|
366
|
+
|
|
367
|
+
##### Note: returned `validator name` as key is key for define validation message to this validator in configuration.
|
|
368
|
+
For example for described above cases you will use `range` and `passwordMatch` as keys to define validation message.
|
|
369
|
+
|
|
370
|
+
### Example custom validator
|
|
371
|
+
Suppose you write **range** validator to check that input string length satisfies the specified range.
|
|
372
|
+
Your validator takes 2 parameters **min** and **max** and compare with string length, if string length doesn't pass condition you need to return error object which contains passed **min**, **max** params:
|
|
373
|
+
|
|
374
|
+
```typescript
|
|
375
|
+
//...
|
|
376
|
+
|
|
377
|
+
function range(min: number, max: number): ValidatorFn {
|
|
378
|
+
return (control: AbstractControl): ValidationErrors => {
|
|
379
|
+
if (min === undefined || max === undefined) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const value = control.value as string;
|
|
383
|
+
if (!value) {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (value.length < min || value.length > max) {
|
|
388
|
+
return {
|
|
389
|
+
// Use validator name as key for returning object it
|
|
390
|
+
// will use to define validation message
|
|
391
|
+
range: {
|
|
392
|
+
// Put passed params to validator error answer and you can access to this params
|
|
393
|
+
// in validation message use #[min], #[max] placeholders
|
|
394
|
+
max,
|
|
395
|
+
min
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return undefined;
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
//...
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
After that you can define validation message for range validator like this:
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
//...
|
|
411
|
+
|
|
412
|
+
NgxValidationMessagesModule.forRoot({
|
|
413
|
+
messages: {
|
|
414
|
+
// You use validator name as range because you return error object with key 'range'
|
|
415
|
+
// also you can use params placeholders returned in error 'range' object
|
|
416
|
+
range: 'The string must be at least #[min] and no more than #[max] characters.',
|
|
417
|
+
}
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
//...
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
## Further improvements
|
|
424
|
+
|
|
425
|
+
- Apply server validation messages to form controls
|