Hello,
the FormControl validators are implemented as a service, as they must use other custom services, which serve the error messages. Somehow the service injected in the Validation service is undefined, and I have no idea what is going on...
ERROR TypeError: Cannot read properties of undefined (reading '_linkErrorMessageService'
export interface ErrorMessageService<T> {
getMessaghe(key: T): ValidationErrors;
}
@Injectable({
providedIn: 'root',
})
export class LinkErrorMessageService implements ErrorMessageService<LinkErrorKeys> {
getMessaghe(key: LinkErrorKeys): ValidationErrors {
return { key: linkErrors[key] };
}
}
@Injectable({
providedIn: 'root',
})
export class LinkValidationService {
constructor(private _linkErrorMessageService: LinkErrorMessageService) {}
validUrlFormat(control: AbstractControl): ValidationErrors | null {
if (control.value) {
const error: ValidationErrors = this._linkErrorMessageService.getMessaghe(LinkErrorKeys.invalidUrl); //undefined
try {
new URL(control.value);
const topLevelDomainRegex: RegExp = /.\../;
if (!topLevelDomainRegex.test(control.value)) {
return error;
}
} catch (e) {
return error;
}
}
return null;
}
💯