My goal is to eliminate the specific validator from the validator array so that I can reconfigure the controls when certain values have changed.
I am aware of the traditional solution where I would need to repeatedly set validators.
checked(event: MatCheckboxClickAction): void {
const control = (this.form.get(
'information',
) as FormGroup).controls.data1;
if (event) {
this.updateRequiredValidator(control);
} else {
control.setValidators([
Validators.maxLength(9), Validators.minLength(2)
]);
control.updateValueAndValidity();
}
}
updateRequiredValidator(control: AbstractControl): void {
control.setValidators([
Validators.required,
...(control?.validator ? [control?.validator as ValidatorFn] : []),
]);
control.updateValueAndValidity();
}
Instead of repeatedly setting validators, I simply want to remove Validators.required on the else part.