question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attempting to Bind Two Dependent Inputs Disabled State in Reactive Forms Results in Stack Overflow

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Attempting to bind two dependent inputs disabled state in a reactive form results in a stack overflow.

Expected behavior

(Given input A and B)

Input A is disabled when B is present, and B is disabled when A is present.

Minimal reproduction of the problem with instructions

Example minimal plunkr: http://plnkr.co/edit/30TjtFLSf7BmvElYTByf?p=preview

What is the motivation / use case for changing the behavior?

When constructing a form, it is very common to disable one input based on the presence of another. This is possible with template forms, but it is not possible with model forms. Issue #11271 suggests the only way to do this is by subscribing to the valueChanges property of a FormControl and manually calling FormControl#disable. However, this does not work because changes to input A’s disabled state will infinity trigger input B’s valueChanges subscribers and so on.

Please tell us about your environment:

  • Angular version: 2.0.X
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version =

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
noamichaelcommented, Dec 22, 2016

@pmulac My case seems simple now compared to yours! Have you looked into AbstractControl#updateValueAndValidity? You may be able to use it to manually update your subforms validity and disabled state instead of relying on{ emitEvent: true }.

I also think this use case can be made generic, like mine, so that you can just pass a tree structure into a function which will manage creating all the subscribers for you.

I love Angular 2, but I think we need better documentation on complex form definitions.

2reactions
DzmitryShylovichcommented, Dec 21, 2016

FormControl.disable method accepts sevelar args see link so u can disable it without triggering subscriptions. Example http://plnkr.co/edit/df3mlu6HXtNHPGb3pHJv?p=preview

Read more comments on GitHub >

github_iconTop Results From Across the Web

dynamically disable input with getter on Reactive Forms?
In this case name: this.name = this.fb.control({ value: '', disabled: this.isNameDisabled }, this.nameValidations ) will work only to ...
Read more >
48 answers on StackOverflow to the most popular Angular ...
I gathered the most common questions and answers from Stackoverflow. These questions were chosen by the highest score received.
Read more >
I changed my implementation of an EXTREMELY deeply ...
I changed my implementation of an EXTREMELY deeply nested Angular Reactive Form and you won't believe what happened. Do you have a deeply...
Read more >
Introduction to forms in Angular
Setting up the form modellink ... Both reactive and template-driven forms track value changes between the form input elements that users interact with...
Read more >
Angular Reactive Forms: Tips and Tricks
For example, if you have an input that is bound to a form control, Angular performs the control validation process for every keystroke....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found