Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveForms: FormGroup.removeControl doesn't call control.setParent(null) #29517

Open
mbinic opened this issue Mar 26, 2019 · 4 comments
Open
Labels
area: forms P4 A relatively minor issue that is not relevant to core functions
Milestone

Comments

@mbinic
Copy link

mbinic commented Mar 26, 2019

馃悶 bug report

Affected Package

@angular/forms

Is this a regression?

Most probably no.

Description

The FormGroup class' removeControl method does not call setParent(null) for the target control. This appears to be an inconsistency in the implementation.

Our expectation is that removeControl would call control.setParent(null), to clear the control's parent once detached. This would also make it symmetric with addControl.

Use case

We have a dynamic form implementation where a class extending FormGroup is dynamically attaching/detaching child groups/controls using addControl/removeControl. The controls can react to their inclusion/exclusion in their setParent method (to e.g. subscribe to a parent's attachedState observable).

Since we're extending FormGroup this is easy to work around by just overriding removeControl, but IMHO the inconsistency in the framework should be fixed.

馃敩 Minimal Reproduction

The call is missing from: https://github.com/angular/angular/blob/master/packages/forms/src/model.ts#L1298 .

馃敟 Exception or Error

None.

馃實 Your Environment

Angular Version: 7.2.0


Angular CLI: 7.3.0
Node: 10.15.3
OS: win32 x64

Anything else relevant?

No.

@mbinic
Copy link
Author

mbinic commented Mar 28, 2019

Trying to fix this, I just noticed that setControl, which replaces a control, doesn't call setParent(null) on the old control either: https://github.com/angular/angular/blob/master/packages/forms/src/model.ts#L1310.

Also found a reference to issue #24571 on the _parent field declaration, which I think can be initialized to null, since it would initially be undefined even though that's not listed as one of its possible types; and also cause setParent(undefined) somehow feels wrong...
I come from the C# world though, so please feel free to correct me if something like that feels more at home here. In the meantime I'll make the change locally, add tests and see if anything blows up :)

mbinic added a commit to mbinic/angular that referenced this issue Mar 28, 2019
When a control is removed from a FormGroup, or replaced by another control, that control's parent
wasn't cleared. Apart from leaving the parent reference on the removed control, this also made it
impossible to react on the control being removed from an overridden setParent.

This change introduces an unregisterControl method, symmetric to registerControl, which is called
to clear a control's parent (set to null) when it's removed or replaced.

PR Close angular#29517
mbinic added a commit to mbinic/angular that referenced this issue Mar 29, 2019
When a control is removed from a FormGroup, or replaced by another control, that control's parent
wasn't cleared. Apart from leaving the parent reference on the removed control, this also made it
impossible for the control to react on its removal in an overridden setParent.

This change introduces an unregisterControl method, symmetric to registerControl, which is called
to clear a control's parent (set to null) when it's removed or replaced.

PR Close angular#29517
@Airblader
Copy link
Contributor

@mbinic Sounds all fine to me.

@mbinic
Copy link
Author

mbinic commented Mar 29, 2019

Sorry @Airblader, I don't understand - are you saying that the original behavior is fine (i.e. no change is needed), or that assigning undefined to parent is fine, or that the changes my PR introduces are fine? Not all three can be fine at the same time :)

@Airblader
Copy link
Contributor

That your PR changes sound fine :-)

@ngbot ngbot bot modified the milestones: needsTriage, Backlog Sep 12, 2019
@ngbot ngbot bot modified the milestones: Backlog, needsTriage Jun 2, 2020
@jelbourn jelbourn added the P4 A relatively minor issue that is not relevant to core functions label Oct 21, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests

7 participants