I created a unique custom validator that works like this:
export function checkValidity(control: AbstractControl, shouldValidate: boolean,
errorDetails: { [key: string]: boolean }): null | { [key: string]: boolean } {
const valueToCheck: string = control.value;
if (!valueToCheck || shouldValidate) {
return null;
} else {
return errorDetails;
}
}
It's quite simple and easy to understand: It extracts the value from the form control and returns null if the value is defined or meets the given condition in a parameter, otherwise it returns an error object.
Now I want to use this custom validator on a control, but I'm not sure how to pass the current AbstractControl
. I attempted something like this:
private formDataBuilder: FormBuilder = new FormBuilder();
public createForm(formData: SomeType): FormGroup {
return this.formDataBuilder.group({
days: [formData.days],
...
useDefaultRule: [formData.useDefaultRule],
urls: this.formBuilder.group({
webUrl: [formData.urls.webUrl, [checkValidity(new FormControl(),
hasWebURLPattern(new FormControl().value),
{webUrl: true})]]
})
});
}
Unfortunately, this approach doesn't work. How can I correctly pass the current form control as a parameter?