Note - I included a stackblitz link of this at the end.
I am having an issue with my code where I want a formcontrol to essentially be disabled if another formcontrol is currently populated. Example, if field A is populated then input fields B and C will be disabled. If field A is cleared out, then field B and C will re-enable. This occurs on all of the three fields in their respective order meaning if you populate B then A and C will be disabled, etc.
this.SearchForm.get('a').valueChanges
.subscribe( ( value ) => {
if ( value ) {
this.SearchForm.get('b').disable();
this.SearchForm.get('c').disable();
} else {
this.SearchForm.get('b').enable();
this.SearchForm.get('c').enable();
}
});
If I only setup this on one field, this works great. However, if I change it to include setting up the other fields as followed:
this.SearchForm.get('a').valueChanges
.subscribe( ( value ) => {
if ( value ) {
this.SearchForm.get('b').disable();
this.SearchForm.get('c').disable();
} else {
this.SearchForm.get('b').enable();
this.SearchForm.get('c').enable();
}
});
this.SearchForm.get('b').valueChanges
.subscribe( ( value ) => {
if ( value ) {
this.SearchForm.get('a').disable();
this.SearchForm.get('c').disable();
} else {
this.SearchForm.get('a').enable();
this.SearchForm.get('c').enable();
}
});
this.SearchForm.get('c').valueChanges
.subscribe( ( value ) => {
if ( value ) {
this.SearchForm.get('a').disable();
this.SearchForm.get('b').disable();
} else {
this.SearchForm.get('a').enable();
this.SearchForm.get('b').enable();
}
});
I get the "Maximum call stack size exceeded" error. Maybe there is a better way of doing this with reactive forms, but I haven't come across anything yet. Part of the reason I took this approach is because once the field is empty it will re-enable the other fields easily with less code.
Link of Stackblitz If you comment out the valueChanges of b and c and just have one, you can see the kind of behavior I am looking for, but if you add even one additional valuechanges it gets the error.