I am trying to make a object which has all the error message and upon getting error in the form with show the message directly, don't have to make any changes in template.
But I am not able to access they validationMessage key using the forEach() loop on the form which I created.
// This object contains all the validation messages for this form
validationMessages = {
'fullName': {
'required': 'Full Name is required.',
'minlength': 'Full Name must be greater than 2 characters.',
'maxlength': 'Full Name must be less than 10 characters.',
},
'email': {
'required': 'Email is required.',
},
'skillName': {
'required': 'Skill Name is required.',
},
'experienceInYears': {
'required': 'Experience is required.',
},
'proficiency': {
'required': 'Proficiency is required.',
},
};
this above object is validation error message
this.employeeForm = this.fb.group({
'fullName': [
'',
[
Validators.required,
Validators.maxLength(15),
Validators.minLength(5),
],
],
'email': ['', [Validators.required, Validators.email]],
'skills': this.fb.group({
'skillName': ['', [Validators.required]],
'experienceInYears': ['', [Validators.required]],
'proficiency': ['', [Validators.required]],
}),
});
above is how my form constructed
logValidationErrors(group: FormGroup): void {
Object.keys(group.controls).forEach((key: string) => {
const abstractControl = group.controls?.[key];
if (abstractControl instanceof FormGroup) {
this.logValidationErrors(abstractControl);
} else {
if (abstractControl && !abstractControl.valid) {
const message = this.validationMessages[key] <-- upon trying to assign the message I am getting error
}
}
});
how to achieve the above as task? I changed the key type to any but also no success. thanks in advance