I've built a simple form using Angular Reactive Forms. I have a custom Validator function, which checks the format of a phone number input. I want to allow an empty input if the users chooses not to enter a phone number, but then validate their entry if they enter one.
To achieve this, I subscribe to the value changes on the form control and use the setValidators method to toggle the validation if the field is empty or not:
phoneControl.valueChanges.subscribe(() => {
if (phoneControl.value === "") {
phoneControl.setValidators(null);
} else {
phoneControl.setValidators(this.phoneValidator());
}
});
Please see the full code on StackBlitz.
The code works initially, form.valid is true. When I enter a number it turns on the validation and then successfully validates it according to my RegEx and shows an error.
However, when I empty the field, the error does not disappear and the form form is still invalid, even though my code should set the validator to null. You can see this in the StackBlitz example above.
I can't figure out why this is happening. Any ideas?