You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide addControls on FormGroup to conveniently add multiple controls #12747
[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Currently, if you want to add a lot of controls to a form group you have to use addControl multiple times.
Expected behavior
Provide an addControls property on FormGroup with the following signature: addControls(dir: FormControlName[]): FormControl;
What is the motivation / use case for changing the behavior?
Instead of having to write:
you could write: formGroup.addControls(someArray);
Do you think that would actually be helpful and worth to implement? I am aware that it is just for convenience. But It seems to be a quite small and easy change, so I would give it a try and work on a PR.
Best
Reacted by Dominik, Smaïl Hammour, June Rockwell and Hanna
I figured but was just checking; some people don't realize that it's there.
I was looking at your PR. One thing i noticed (and it's not actually due to your changes) is that if a control is added with an existing name, nothing happens. This may be awkward when using the new methods; as some controls would be added but others wouldn't.
One option is to run a check first and throw an error. At least the user will have some feedback.
This would make the new method inconsistent with the existing singular version though; and changing the existing one would cause an API incompability issue.
@bzums Thanks for taking the time to create an issue. I agree this is a relatively common use case. However, adding multiple controls is easy enough to accomplish with a simple loop (one line of code in your PR, for example). If we wanted to add this functionality to the API, for symmetry we'd also have to add a removeControls method and complementary addControls and removeControls methods for FormArrays. This is quite a bit to add to the public API for something that is so simple. We'd like to keep the API surface small if possible. For this reason, we've decided not to add the method to the framework at this time. That said, you can certainly add this as a utility method in your projects if you'd like to use it. If you have any other API ideas, we are happy to take a look.
Activity
bzums commentedon Nov 7, 2016
Ok, my suggested signature does not make sense since the
formControlof theformControlNameto add is returned.So in the case of
addControlsthe return value should be an array offormControlI suggest, what do you think?clydin commentedon Nov 7, 2016
Have you tried using FormBuilder? Unless you're doing some type of dynamic addition to the group.
bzums commentedon Nov 9, 2016
@clydin The form is dynamically build on runtime so as you said, that is not possible.
clydin commentedon Nov 9, 2016
I figured but was just checking; some people don't realize that it's there.
I was looking at your PR. One thing i noticed (and it's not actually due to your changes) is that if a control is added with an existing name, nothing happens. This may be awkward when using the new methods; as some controls would be added but others wouldn't.
bzums commentedon Nov 9, 2016
@clydin Good point, any suggestions how to handle that scenario?
feat(forms): `addControls` in `FormGroup` to add multiple controls at…
clydin commentedon Nov 9, 2016
One option is to run a check first and throw an error. At least the user will have some feedback.
This would make the new method inconsistent with the existing singular version though; and changing the existing one would cause an API incompability issue.
kara commentedon Nov 10, 2016
@bzums Thanks for taking the time to create an issue. I agree this is a relatively common use case. However, adding multiple controls is easy enough to accomplish with a simple loop (one line of code in your PR, for example). If we wanted to add this functionality to the API, for symmetry we'd also have to add a
removeControlsmethod and complementaryaddControlsandremoveControlsmethods forFormArrays. This is quite a bit to add to the public API for something that is so simple. We'd like to keep the API surface small if possible. For this reason, we've decided not to add the method to the framework at this time. That said, you can certainly add this as a utility method in your projects if you'd like to use it. If you have any other API ideas, we are happy to take a look.angular-automatic-lock-bot commentedon Sep 10, 2019
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.