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