I have created a StackBlitz example of my problem.
I have a FormService that builds a form, which is obtained by the component in the initForm() method:-
public getForm() {
return this.fb.group({
cb1: this.fb.control(false),
cb2: this.fb.control(false),
inputBox: this.fb.control({value: '', disabled: true}, Validators.required)
});
}
I need to disable the input text box if none of the checkboxes is selected. Which brings me to do something like this :-
if(!this.cb1.value && !this.cb2.value) {
this.isCheckboxSelectionInvalid = true;
this.inputBox.disable();
} else {
this.isCheckboxSelectionInvalid = false;
this.inputBox.enable();
}
This works for all default scenarios - however there is a problem. When I call resetForm() (provided the input is enabled by checking one of the checkboxes) which basically calls the same method of initForm(), the input stays enabled. This is even after I call the method that validates checkbox selection and disables the input in the initForm() method.
For sanity, I logged the value of the input's disabled property - which logs true.
Why is it that the input doesn't get disabled then? Any help would be very appreciated, this is a problem at work.