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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(forms): Don't override form group's dirty state when disabling controls #24591

Closed
wants to merge 2 commits into from

Conversation

martinsik
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[X] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

form = this.fb.group({ a: ['a'], b: ['b'] });
form.markAsDirty();
console.log(form.dirty); // true

form.controls['a'].disable();
console.log(form.dirty); // false

I can manually change dirty state of a FormGroup but when I disable a single control it changes dirty state of its parent.

Demo: https://stackblitz.com/edit/angular-8bab4v?file=src%2Fapp%2Fapp.component.ts

Issue Number: #23309

What is the new behavior?

When calling disable() on AbstractControl it now checks whether its parent is dirty and if its dirty state corresponds to the current state of its children. If not it means I had to change it myself with markAsDirty so I won't override parent's dirty state.

Does this PR introduce a breaking change?

[ ] Yes
[X] No

This could be a BC but it's probably very unlikely that anybody was relying on this behavior since its counter-intuitive.

@mhevery mhevery requested a review from kara July 12, 2018 22:53
@kara kara added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jul 31, 2018
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the bug fix! Some comments below.

packages/forms/src/model.ts Outdated Show resolved Hide resolved
@@ -491,10 +494,13 @@ export abstract class AbstractControl {
this._onDisabledChange.forEach((changeFn) => changeFn(false));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like enable() needs to be updated with the check as well. Otherwise, you'll have the same problem of resetting dirtiness when you re-enable the control.

expect(c.dirty).toBe(false);

c.disable();
expect(a.dirty).toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the following to this test (as well as the other one)?

c.enable();
expect(a.dirty).toBe(true);

I think this assertion would fail as the code is currently written.

@kara kara added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Dec 19, 2018
@ngbot ngbot bot added this to the needsTriage milestone Dec 19, 2018
@martinsik martinsik requested a review from a team as a code owner January 9, 2019 09:41
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

@googlebot googlebot added cla: no and removed cla: yes labels Jan 9, 2019
@martinsik
Copy link
Contributor Author

@kara I finally managed to run Bazel so I'll have a look at this ASAP. I've actually completely forgotten what I did here because it's been 6 months now so I'll need to recall what's going on in this PR.

@kara
Copy link
Contributor

kara commented Jan 10, 2019

@martinsik No worries, sorry for the initial delay. Ping me whenever it's ready and I'll have better turnaround time this time :-)

martinsik added a commit to martinsik/angular that referenced this pull request Jan 11, 2019
martinsik added a commit to martinsik/angular that referenced this pull request Jan 11, 2019
@martinsik martinsik force-pushed the issue-23309 branch 2 times, most recently from 588243c to 7ffb45a Compare January 11, 2019 12:07
@martinsik
Copy link
Contributor Author

@kara I updated this PR. You were right, it really didn't work after calling enable() on a form control but it should be fixed now. I also added the test case you suggested.

I think I messed the history in this PR when I unintentionally referenced this PR id instead of the issue id but it's showing just 3 commits here https://github.com/angular/angular/pull/24591/commits which is correct.

@martinsik
Copy link
Contributor Author

I don't know why CLA is failing. Maybe because I merged one suggestion as a comit here on GitHub.

@kara kara added cla: yes and removed cla: no labels Jan 17, 2019
@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small nit, then should be good to go.

Can you squash your commits together into one? I think the commit in the middle is causing our linter job to fail.

packages/forms/src/model.ts Outdated Show resolved Hide resolved
martinsik and others added 2 commits January 24, 2019 16:06
…ntrols

Update packages/forms/src/model.ts

Co-Authored-By: martinsik <martin.sikora.ahoj@gmail.com>
@googlebot
Copy link

CLAs look good, thanks!

@kara
Copy link
Contributor

kara commented Jan 25, 2019

@martinsik FYI, I squashed the commits and fixed the nit to move the PR along. If Google internal tests pass, this should be good to go.

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kara
Copy link
Contributor

kara commented Jan 25, 2019

presubmit

@kara kara added action: presubmit The PR is in need of a google3 presubmit and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Jan 25, 2019
@kara kara added target: patch This PR is targeted for the next patch release target: major This PR is targeted for the next major release action: merge The PR is ready for merge by the caretaker and removed target: patch This PR is targeted for the next patch release action: presubmit The PR is in need of a google3 presubmit labels Jan 25, 2019
@jasonaden jasonaden closed this in ef67282 Jan 25, 2019
wKoza pushed a commit to wKoza/angular that referenced this pull request Jan 26, 2019
…ntrols (angular#24591)

Update packages/forms/src/model.ts

Co-Authored-By: martinsik <martin.sikora.ahoj@gmail.com>

PR Close angular#24591
wKoza pushed a commit to wKoza/angular that referenced this pull request Jan 26, 2019
…ntrols (angular#24591)

Update packages/forms/src/model.ts

Co-Authored-By: martinsik <martin.sikora.ahoj@gmail.com>

PR Close angular#24591
vetom pushed a commit to vetom/angular that referenced this pull request Jan 31, 2019
…ntrols (angular#24591)

Update packages/forms/src/model.ts

Co-Authored-By: martinsik <martin.sikora.ahoj@gmail.com>

PR Close angular#24591
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cla: yes target: major This PR is targeted for the next major release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants