Angular Validation Tooltip Directive
https://piyalidas10.github.io/validation-tooltip-directive/
ng serve
http://localhost:4200
fieldInfoContent.json in assets folder
{
"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"
}
}
validation-msg.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ValidationMessageService {
validationErrorObj = [];
public getValidationMsg(validationId: string): string {
return this.validationErrorObj[validationId];
}
}
validationErrorMsg() {
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.