Skip to content

Conversation

@Bjeaurn
Copy link
Contributor

@Bjeaurn Bjeaurn commented Jul 23, 2024

Enables users to add an array of FormControls to a FormArray using its existing .push() method, instead of pushing each new FormControl one by one triggering events along the way.

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

What is the current behavior?

FormArray .push() currently only accepts a single FormControl or FormGroup, manipulating a FormArray holding large datasets and using expensive validators is complicated by this behavior, as there is no easy way to refresh the dataset without breaking existing streams depending on its .valueChanges.

Issue Number: N/A

What is the new behavior?

FormArray .push() now accepts both a single FormControl or FormGroup, but also accepts an array of the same types. In case of an array, it will register all controls as expected and emit a single event after.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Ran into this issue myself working at a client, where we could not easily manipulate a FormArray where we want to refresh a large dataset. Swapping it out for a new FormArray() seemed like the way, but this broke any existing streams subscribed to its original .valueChanges, making it difficult to keep everything in sync and reactive.

This solution at least makes it possible to manipulate a dataset within a FormArray as a new dataset, instead of having to iterate over all new controls. A possible expansion on this solution, and to promote immutability, could be to offer an additional method that also internally clears the existing dataset so a full replace would be possible.

Additionally, managed to run the entire test suite locally, except for //packages/zone.js/test:karma_jasmine_test_ci_chromium which seemed to not be able to connect to its ChromeHeadless browser and therefore fail. Hopefully the CI can run this test for me, but I'd still like to know what went wrong exactly. Short excerpt:

...
//packages/zone.js/test:test_node_error_lazy_policy             (cached) PASSED in 0.9s
//packages/zone.js/test:test_npm_package                        (cached) PASSED in 0.7s
//packages/zone.js/test/zone-spec/clock-tests:test_patched      (cached) PASSED in 0.5s
//packages/zone.js/test/zone-spec/clock-tests:test_unpatched    (cached) PASSED in 0.4s
//packages/zone.js/test:karma_jasmine_test_ci_chromium                   FAILED in 541.3s

@pullapprove pullapprove bot requested a review from AndrewKushnir July 23, 2024 17:52
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: forms labels Jul 23, 2024
@ngbot ngbot bot added this to the Backlog milestone Jul 23, 2024
@thePunderWoman thePunderWoman marked this pull request as draft May 21, 2025 15:28
@thePunderWoman thePunderWoman removed the request for review from AndrewKushnir June 3, 2025 11:02
@Bjeaurn
Copy link
Contributor Author

Bjeaurn commented Jul 23, 2025

Is there anything I can do, add or expand upon to make this something we want to consider and/or merge?

@Bjeaurn
Copy link
Contributor Author

Bjeaurn commented Aug 1, 2025

@MarkTechson As requested on youtube! Sorry to bother.

Enables users to add an array of FormControls to a FormArray using its existing .push() method,
instead of pushing each new FormControl one by one triggering events along the way.
@JeanMeche JeanMeche force-pushed the formarray-add-push-arrays branch from 9328c06 to 9757f20 Compare August 1, 2025 19:07
@MarkTechson MarkTechson requested a review from kirjs August 1, 2025 19:45
@MarkTechson MarkTechson added the action: review The PR is still awaiting reviews from at least one requested reviewer label Aug 1, 2025
@JeanMeche
Copy link
Member

As a curiosity, I ran a TGP which returned green.

@JeanMeche JeanMeche marked this pull request as ready for review August 3, 2025 17:19
@pullapprove pullapprove bot requested review from crisbeto, devversion and kirjs August 5, 2025 09:35
Copy link
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

@JeanMeche JeanMeche added the target: minor This PR is targeted for the next minor release label Aug 5, 2025
@kirjs
Copy link
Contributor

kirjs commented Aug 5, 2025

The internal failing tests seems to be a flake? Let me find out how to re-run

@kirjs kirjs added the action: presubmit The PR is in need of a google3 presubmit label Aug 5, 2025
Copy link
Contributor

@kirjs kirjs left a comment

Choose a reason for hiding this comment

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

Approved-for: public-api

The code looks great, thank you for your contribution

@pullapprove pullapprove bot requested a review from atscott August 5, 2025 09:39
Copy link
Contributor

@kirjs kirjs left a comment

Choose a reason for hiding this comment

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

reviewed-for: public-api

The code looks great, thank you for your contribution

@JeanMeche JeanMeche removed action: review The PR is still awaiting reviews from at least one requested reviewer action: presubmit The PR is in need of a google3 presubmit labels Aug 5, 2025
@JeanMeche JeanMeche added the action: merge The PR is ready for merge by the caretaker label Aug 5, 2025
@crisbeto
Copy link
Member

crisbeto commented Aug 6, 2025

This PR was merged into the repository by commit c353497.

The changes were merged into the following branches: main

@crisbeto crisbeto closed this in c353497 Aug 6, 2025
@Bjeaurn Bjeaurn deleted the formarray-add-push-arrays branch August 6, 2025 09:33
@flensrocker
Copy link

Isn't this a breaking change if I have an array of arrays?

@MarkTechson
Copy link
Contributor

Isn't this a breaking change if I have an array of arrays?

Do you have a repro you can share @flensrocker?

@flensrocker
Copy link

I can try to create one this evening (timezone CET).
As I think it depends, if Array.isArray is true for FormArray.

crisbeto added a commit to crisbeto/angular that referenced this pull request Aug 6, 2025
@JeanMeche
Copy link
Member

JeanMeche commented Aug 6, 2025

As I think it depends, if Array.isArray is true for FormArray.

FormArray isn't an array, so we should be good.

If but curious if you can find a breaking usecase.

@flensrocker
Copy link

I guess I was "overreacting", but my experience let me bring this up, because I also don't always think about corner cases.

Yes, I can confirm that FormArray is not an array in the sense of Array.isArray (as no provided AbstractControl is).

Now I'm thinking about, if it's possible to subclass an own AbstractControl, which is an array... 🤔

I will investigate, but I don't think I find a breaking change.

Thanks for listening!

@Bjeaurn
Copy link
Contributor Author

Bjeaurn commented Aug 7, 2025

In case you come up with a case @flensrocker, let me know. I'll dive into Angular Forms API to see if they have a native way of detecting if it's a FormArray or a regular array and see if we can handle both situations as needed. But I think, considering the original API has been expanded from just accepting a TControl, the surface of changes for existing usage isn't that big.

Or at least I would hope so! 😄

@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 7, 2025
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 detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants