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

feat(forms): add markAllAsDirty to AbstractControl #58663

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

aberonni
Copy link

Adds the markAllAsDirty method to the AbstractControl class. This method will mark the control and all its descendants as dirty.

I pretty much just duplicated the behaviour and tests of markAllAsTouched.

Fixes #55990

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • 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.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #55990

What is the new behavior?

Adding markAllAsDirty to AbstractControl

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

N/A

@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: forms labels Nov 14, 2024
@ngbot ngbot bot added this to the Backlog milestone Nov 14, 2024
@aberonni aberonni marked this pull request as ready for review November 14, 2024 16:19
@pullapprove pullapprove bot requested a review from AndrewKushnir November 14, 2024 16:19
Adds the `markAllAsDirty` method to the `AbstractControl` class. This method will mark the control and all its
descendants as dirty.

I pretty much just duplicated the behaviour and tests of `markAllAsTouched`.

Fixes angular#55990
@aberonni aberonni force-pushed the aberonni/add-markAllAsDirty branch from 250f823 to 587a1c8 Compare November 14, 2024 16:22
@angular angular deleted a comment from google-cla bot Dec 16, 2024
@aberonni
Copy link
Author

@AndrewKushnir just checking if this requires any action from me?


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

formArray.markAllAsDirty();
Copy link
Member

@JeanMeche JeanMeche Jan 1, 2025

Choose a reason for hiding this comment

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

Can we add tests that also cover emitEvent ? (and checking the events are emitted)

Copy link
Author

Choose a reason for hiding this comment

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

I've added a basic test which I grabbed from elsewhere for inspiration. I also did the same for markAllAsTouched which is what I got inspiration from for the other tests.


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

const group1Control1 = group1.get('c1') as FormControl;
Copy link

@michael-small michael-small Jan 1, 2025

Choose a reason for hiding this comment

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

This question may be bigger than this PR's scope:

I see that the old fashioned .get('...') as FormXXX conventions come from existing tests. But I wonder: is there any reason for this now other than consistency? I would think they could be properly typed now by just accessing the now default typing. Either just for these new tests now, or perhaps someone take a more formal sweep through form tests in a separate PR.

Copy link
Author

Choose a reason for hiding this comment

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

Tbh I just followed what was in the tests for markAllAsTouched. This is my first contribution to angular so I'm not sure how else to do things, but happy to adjust if this is deemed within the scope of my contribution.

Copy link

github-actions bot commented Jan 1, 2025

Deployed adev-preview for 587a1c8 to: https://ng-dev-previews-fw--pr-angular-angular-58663-adev-prev-qaaol9d0.web.app

Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt.

@aberonni aberonni force-pushed the aberonni/add-markAllAsDirty branch from 0f78d85 to 6ce833f Compare January 2, 2025 08:50
Copy link

@michael-small michael-small left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -141,6 +221,12 @@ import {StatusChangeEvent} from '../src/model/abstract_model';

expect(arrayFirstChildGroupFirstChildCtrl.touched).toBe(true);
});

it('should not emit events when `FormGroup.markAllAsTouched` is called with `emitEvent: false`', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this test.
Consider also adding a test that verifies that event is emitted for true and not having a value.

* When false, no events are emitted.
*/
markAllAsDirty(opts: {emitEvent?: boolean} = {}): void {
this.markAsDirty({onlySelf: true, emitEvent: opts.emitEvent, sourceControl: this});
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering why onlySelf is not being passed as a prop to cover the case where we want to mark descendants but not ascendance?

I assume this is just something you copypasted from markAllAsTouched, but curious if you have any context for this.

expect(arrayFirstChildGroupFirstChildCtrl.dirty).toBe(true);
});

it('should not emit events when `FormGroup.markAllAsDirty` is called with `emitEvent: false`', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a test that verifies that event is emitted for true and not having a value.

*
* @param opts Configuration options that determine how the control propagates changes
* and emits events after marking is applied.
* * `emitEvent`: When true or not supplied (the default), the `events`
Copy link
Contributor

Choose a reason for hiding this comment

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

image Looking at the code, I'm not convinced that true and missing value would do the same thing, but a test would help clarify that

Copy link
Member

Choose a reason for hiding this comment

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

undefined is !== false.
I think we're good here !

@aberonni
Copy link
Author

@kirjs thanks for the review. I'm not sure how to proceed on this, given that the PR seems to have the necessary approvals, but there are also change requests. I also want to point out that the requested changes seem to be beyond the scope of my addition - given that they are addressing feedback on tests that were already there. If it helps get this merged, I can put more time and effort in, but I doubt I am the best suited to put that effort in (I'm a first-time contributor, and just trying to add a very small change).

@aberonni
Copy link
Author

aberonni commented Feb 3, 2025

@AndrewKushnir @kirjs @michael-small can you let me know if this needs any action from me or whether we can proceed as is?

@michael-small
Copy link

michael-small commented Feb 3, 2025

I'm just a random community guy who keeps up with forms PRs. I think these changes are legit. My comments were just thinking out loud to real maintainers about the existing tests beyond the scope of your PR.

Looks good to me for what it's worth

@JeanMeche
Copy link
Member

Pullapprove is reporting that we miss 2 public-api scope approvals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow c.markAllAsDirty() like c.markAllAsTouched() for AbstractControl class
5 participants