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