Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
# Validation-Directive-in-Angular-6
# Validation-Tooltip-Directive-in-Angular-6

## RUN Application

```
ng serve --port 4201
http://localhost:4201
ng serve
http://localhost:4200
```

## Mockdata
validationerrors.json in assets folder
fieldInfoContent.json in assets folder

```
{
"validationErrors": {
"firstName-required" : "Firstname is a required field",
"firstName-minlength" : "Firstname must have 2 characters",
"firstName-maxlength" : "Firstname can have maximum 30 characters",

"lastName-required" : "Lastname is a required field",
"lastName-minlength" : "Lastname must have 2 characters",
"lastName-maxlength" : "Lastname can have maximum 30 characters",

"emailId-required": "Email is a required field",
"emailId-email": "Email is not in valid format",
"emailId-minlength" : "Email must have 6 characters",

"mobile-required" : "Mobile is a required field",
"mobile-minlength" : "Mobile must have 10 characters",

"password-required" : "Password is a required field",
"password-minlength" : "Password must have 6 characters",
"password-maxlength" : "Password can have maximum 20 characters"
"fieldInfo": {
"firstName" : "Firstname is a required field",
"middleName": "middleName is not a required field",
"lastName" : "Lastname is a required field",
"emailId": "Email is a required field",
"mobile" : "Mobile is a required field",
"password" : "Password is a required field"
}
}
```
Expand All @@ -52,22 +40,23 @@ export class ValidationMessageService {
}
```

###Load all messages from validationerrors.json using the following code which is written in register.component.ts
###Load all messages from fieldInfoContent.json using the following code which is written in register.component.ts

```
validationErrorMsg() {
this.apiService.getValidationErrorMessage().then(
(res) => {
if (this.validErrorMsgService.validationErrorObj.length === 0) {
this.validErrorMsgService.validationErrorObj = res['validationErrors'];
console.log('Validation Error => ', this.validErrorMsgService.validationErrorObj);
}
}, (error) => {
this.errorData = this.sharedService.getErrorKeys(error.statusText);
});
this.apiService.getFieldInfoMessage().then(
(res) => {
if (this.validErrorMsgService.validationErrorObj.length === 0) {
this.fieldInfoMsgArr = res['fieldInfo'];
console.log('Field Info Array => ', this.fieldInfoMsgArr);
this.isLoading = false;
}
}, (error) => {
console.log(error);
this.isLoading = false;
});
}
```

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.

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.
Binary file removed download.png
Binary file not shown.
Loading