Skip to content

Provide addControls on FormGroup to conveniently add multiple controls #12747

Closed

Description

@bzums

I'm submitting a ... (check one with "x")

[ ] 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:

formGroup.addControl('controlX', someControlX);
formGroup.addControl('controlY', someControlY);
formGroup.addControl('controlZ', someControlZ);
...

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

Activity

  1. bzums commented on Nov 7, 2016

    @bzums
    Author

    Ok, my suggested signature does not make sense since the formControl of the formControlName to add is returned.

    So in the case of addControls the return value should be an array of formControl I suggest, what do you think?

  2. clydin commented on Nov 7, 2016

    @clydin
    Member

    Have you tried using FormBuilder? Unless you're doing some type of dynamic addition to the group.

  3. bzums commented on Nov 9, 2016

    @bzums
    Author

    @clydin The form is dynamically build on runtime so as you said, that is not possible.

  4. clydin commented on Nov 9, 2016

    @clydin
    Member

    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.

  5. bzums commented on Nov 9, 2016

    @bzums
    Author

    @clydin Good point, any suggestions how to handle that scenario?

  6. added a commit that references this issue on Nov 9, 2016
    5f2d063
  7. clydin commented on Nov 9, 2016

    @clydin
    Member

    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.

  8. added
    feature
    on Nov 9, 2016
  9. kara commented on Nov 10, 2016

    @kara
    Contributor

    @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.

  10. angular-automatic-lock-bot commented on Sep 10, 2019

    @angular-automatic-lock-bot

    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.

  11. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: formsfeature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions