@lagoshny/ngx-validation-messages 1.1.0 → 1.1.4

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