Skip to content

Commit 4370885

Browse files
authored
Update README.md
1 parent d6f9c79 commit 4370885

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,40 @@ validationerrors.json in assets folder
2727
}
2828
}
2929
```
30+
31+
validation-msg.service.ts
32+
33+
```
34+
import { Injectable } from '@angular/core';
35+
36+
@Injectable({
37+
providedIn: 'root'
38+
})
39+
40+
export class ValidationMessageService {
41+
validationErrorObj = [];
42+
public getValidationMsg(validationId: string): string {
43+
return this.validationErrorObj[validationId];
44+
}
45+
}
46+
```
47+
48+
###Load all messages from validationerrors.json using the following code which is written in register.component.ts
49+
50+
```
51+
validationErrorMsg() {
52+
this.apiService.getValidationErrorMessage().then(
53+
(res) => {
54+
if (this.validErrorMsgService.validationErrorObj.length === 0) {
55+
this.validErrorMsgService.validationErrorObj = res['validationErrors'];
56+
console.log('Validation Error => ', this.validErrorMsgService.validationErrorObj);
57+
}
58+
}, (error) => {
59+
this.errorData = this.sharedService.getErrorKeys(error.statusText);
60+
});
61+
}
62+
```
63+
64+
Now i have to create a directive validation-label.directive.ts will run on the status change of the form control elements. It requires a ‘formControlName’ attribute added to the control. This will be used to used to construct the key that will be passed to the validation service class. The above directive will handle the change/blur events on controls and displays the messages accordingly.
65+
66+
Few things to notice in this class are that I defined ‘statusChangeSubscription’ to listen to status changes on the control and remove/add error message depending on the status (INVALID). Also we unsubscribe it on destroy class. And I defined a helper method to construct the message key, get the error message from service class and display it on the view.

0 commit comments

Comments
 (0)