-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FormGroup reset() throws error if a control is removed based on a valueChanges subscription #33401
Comments
Hi @miguelleite, thanks for reporting the issue. It looks like the problem appears because the I will keep this issue open to see if we can improve Thank you. |
This workaround is not what i need, because in my case, i want the control to be removed after the reset. That's why i have that valueChange subscription inthe first place, whenever that value is set to that initial one, the other control should be removed. A simpler "workaround" with that drawback is just using emit: false - which is not what i want in my situation. |
@ThomasKruegl could you please provide a minimal repro using Stackblitz (and also describe expected vs actual behavior) so that we can have a look? Thank you. |
Example: https://stackblitz.com/edit/angular-ivy-iuwf7c Choose "option 1" in the dropdown (shows additional input field), then click the "Rest Form" button -> error in console I have added your workaround, except one line. If you uncomment that line, your workaround is active. No error in console - but the form control is not removed from the FormGroup on reset. It is visible in the UI in my example because my ngIf checks the control, not the value of the other control. I am aware i could solve the display in the UI by using such a check, or a flag variable, but i want the FormGroup to align with what is displayed. |
Hi @ThomasKruegl, thanks for the repro. I believe the solution might be to have more logic in the Thank you. |
Our real form is more complicated. We had a solution like that, but ran into problems because we were basically replicating the add/remove logic in the reset step, which is hard to maintain. Additional problem here is that the form is an Input for child components (working with a stepper). So, yes, there are workarounds, but it would be nice if the simple reset worked ;) |
…ing reset When a form is reset, it goes through `_forEachChild` to call `reset` on each of its children. The problem is that if a control is removed while the loop is running (e.g. by a subscription), the form will throw an error, because it built up the list of available control before the loop started. These changes fix the issue by adding a null check before invoing the callback. Fixes angular#33401.
…ing reset (#40462) When a form is reset, it goes through `_forEachChild` to call `reset` on each of its children. The problem is that if a control is removed while the loop is running (e.g. by a subscription), the form will throw an error, because it built up the list of available control before the loop started. These changes fix the issue by adding a null check before invoing the callback. Fixes #33401. PR Close #40462
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 bug report
Affected Package
The issue is caused by package @angular/formsIs this a regression?
Not sure.Description
On aFormGroup
, we have twoFormControl
, where the second is shown when, and only when first field has a value.To achieve this, we're subscribing to the first control
valueChanges
observable, and if there's no value (and the second control exists), we remove the second one; if there's a value (and the second control doesn't exist yet), we add the second control.We also have a button that allows us to reset the form to the initial state.
Problem occurs when we reset, where an error is thrown. This happens because the second control is still part of the form group's dictionary of named controls, but the value is undefined (and it tries to call 'reset' of undefined).
🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-issue-repro2-p1nwkd🔥 Exception or Error
🌍 Your Environment
Angular Version:
Anything else relevant?
No.
The text was updated successfully, but these errors were encountered: