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

Supporting [formArray] without a [formGroup] parent #30264

Open
WreckItRalph opened this issue May 4, 2019 · 63 comments · May be fixed by #55880
Open

Supporting [formArray] without a [formGroup] parent #30264

WreckItRalph opened this issue May 4, 2019 · 63 comments · May be fixed by #55880
Labels
area: forms effort2: days feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature freq2: medium risk: medium state: Needs Design
Milestone

Comments

@WreckItRalph
Copy link
Contributor

🚀 feature request

Relevant Package

This feature request is for @angular/forms

Description

There should be a [formArray] directive to create a form. Currently, we can defined formArrayName only if has a parent [formGroup] directive.

Describe alternatives you've considered

No work around to do this as of now. If we need a formArray, then a parent formGroup is needed.
@mlc-mlapis
Copy link
Contributor

@WreckItRalph ... what is the advantage of that? Why do you suggest that at all?

@WreckItRalph
Copy link
Contributor Author

WreckItRalph commented May 5, 2019

@mlc-mlapis There might be scenarios where the form that needs to be created is a formArray and does not need a container, like submitting a list of { name, email}. In current scenario, it would look something like :
{ details: [ { name: 'something', email: 'someEmail' } ] }

It would be better if it could be just the array:
[{ name: 'something', email: 'someEmail' }]

@mlc-mlapis
Copy link
Contributor

@WreckItRalph ... yep, I can see that theoretical case but in reality, I have never met it.

@WreckItRalph
Copy link
Contributor Author

Well.. I have met and hence the suggestion. It would be good to have.

@ngbot ngbot bot added this to the Backlog milestone May 6, 2019
@ndraiman
Copy link

ndraiman commented Oct 16, 2019

@WreckItRalph ... yep, I can see that theoretical case but in reality, I have never met it.

It's needed, main use case for me are completely dynamic forms.
Had to wrap my FormArrays in a redundant FormGroup just to use it.

@anwarulislam
Copy link

It's needed. I've met it too. But I found a solution myself. I create a FromArray into FormControl of FormGroup and then send it to server by accessing formControlName you know there I have a full array.

So it's like formgroup.controlname.myArray

@spyter
Copy link

spyter commented Nov 5, 2019

I have multiple places in the current application I am working on where this would be beneficial. As a hacky work around, I am using *ngFor to iterate over the FormArray's controls, then binding them w/the [formGroup] directive. This works, but it's certainly not ideal.

@Mastard69
Copy link

It's actually needed. Same problem as @nexxado, I had to wrap it in a FormGroup.

@blemaire
Copy link

Could we bind the [formGroup] to an ng-container ?

@mlc-mlapis
Copy link
Contributor

@blemaire Sure.

@ElisePatrikainen
Copy link

ElisePatrikainen commented Dec 8, 2019

I had this problem as well. I agree on that this would be useful to have.

@ntziolis
Copy link

ntziolis commented Dec 15, 2019

This is NOT a theoretical use case ein fact we encounter it every day.

Here is the simple version of the use case:

  • main component has a form that has a form array
  • sub component is supposed to handle the form array
  • right now we have to pass both the form as well as the form path (to the array) to the sub component

By having a [formArray] directive we can:

  • pass only form array to the sub component
  • sub component doesn't need to know the form path and can just handle the form array itself

@pavankjadda
Copy link
Contributor

@WreckItRalph ... yep, I can see that theoretical case but in reality, I have never met it.

It's needed, main use case for me are completely dynamic forms.
Had to wrap my FormArrays in a redundant FormGroup just to use it.

Yes, my use case aligns with this

@lajuma
Copy link

lajuma commented Mar 6, 2020

same use case as @ntziolis. Is this a feature you're working on or going to work on in near future? Or is there any workaround to pass formArrays into sub components without the need to pass the reference to the parent group?

@jamesrappazzo
Copy link

I have the same use case as @ntziolis as well.

@kremerd
Copy link

kremerd commented Apr 26, 2020

Totally agree with the described use cases. I needed this a bunch of times myself, and would love to finally see a real [formArray] directive!

For what it's worth, though, you can also pass a FormArray to the [formGroup] directive as a workaround. When using strictInputTypes this has to be cast, but in my experience it works without further problems (Stackblitz).

<form [formGroup]="$any(formArray)" *ngFor="let entry of formArray.controls; let i = index">
  <input [formControlName]="i">
</form>

@hamonCordova
Copy link

I needed this a bunch of times myself, and would love to finally see a real [formArray] directive! (2)

@WilHolt
Copy link

WilHolt commented Jul 7, 2020

omg where that [formArray] directive!!!! for god! we need that

@duhamaher
Copy link

we need it

@ErraticFox
Copy link

Oh come on, this is common knowledge. Bumping for support.

@CesarD
Copy link

CesarD commented Oct 29, 2020

And then let's bump it again! NEEDED! 😠

@sic-sic
Copy link

sic-sic commented Nov 2, 2020

Of course it is a needed feature. I also came across a real situation where it is useful.

@Melmoth-the-Wanderer
Copy link

Melmoth-the-Wanderer commented Nov 12, 2020

Yes, I can relate to usecases described here by others. I think this feature migh simplyfy development a lot.

@WreckItRalph ... yep, I can see that theoretical case but in reality, I have never met it.

Nice empathy there my friend.

@EliseoPlunker
Copy link

Directive to iterate over a FormArray:

@Directive({
  selector: '[ngForArray]',
  standalone: true,
})
export class ngForArrayDirective implements DoCheck{
  formArray!: FormArray;
  length: number = -1;
  proxy: any;
  @Input()
  set ngForArray(value: FormArray) {
    this.formArray = value;
    this.redraw();

  }
  ngDoCheck(): void {
    if (this.length!=this.formArray.controls.length)
    {
      this.length=this.formArray.controls.length;
      this.redraw();
    }
  }
  redraw() {
    this.vcr.clear();
    this.formArray.controls.forEach((num: AbstractControl, index: number) => {
      const viewRef = this.vcr.createEmbeddedView(this.tpl);
      viewRef.context = {
        $implicit:
          num instanceof FormGroup ? (num as FormGroup) : (num as FormControl),
        index: index,
      };
    });
  }
  constructor(
    private vcr: ViewContainerRef,
    private tpl: TemplateRef<any>,
  ) {}
}

Use

  <div *ngForArray="formArray;let group;let i=index" [formGroup]="group">
    <input formControlName="id" />
    <input formControlName="name" /><button (click)="formArray.removeAt(i)">remove</button>
  </div>

<div *ngForArray="formArray2;let control">
    <input [formControl]="control" />
  </div>

stackblitz

@SerjaSnow
Copy link

I had this problem a couple of times, too. Is there any update on this issue except the workarounds?

@ezhupa99
Copy link

ezhupa99 commented Oct 4, 2023

I had this problem a couple of times, too. Is there any update on this issue except the workarounds?

There is no [formArray] directive but you can use [formGroup] and map it to a formArray

let's say

  export class Component {
    formArrayRef = new FormArray( [ new FormGroup( { name: new FormControl(null) } ) ] );
    
    // operations with adding or removing data from the formArrayRef 
    // and let's suppose that the form array contains form groups inside
  }
  <form [formGroup]="formArray">
    <div  *ngFor="let formGroupRef of formArrayRef.controls" [formGroup]="formGroupRef ">
      <input formControlName="name" />
    </div>
  </form>

even tho it's a bit counter intuitive to put a formArray into a [formGroup] it works 🔥

@Eliseo-angular
Copy link

I had this problem a couple of times, too. Is there any update on this issue except the workarounds?

There is no [formArray] directive but you can use [formGroup] and map it to a formArray

let's say

  export class Component {
    formArrayRef = new FormArray( [ new FormGroup( { name: new FormControl(null) } ) ] );
    
    // operations with adding or removing data from the formArrayRef 
    // and let's suppose that the form array contains form groups inside
  }
  <form [formGroup]="formArray">
    <div  *ngFor="let formGroupRef of formArrayRef.controls" [formGroup]="formGroupRef ">
      <input formControlName="name" />
    </div>
  </form>

even tho it's a bit counter intuitive to put a formArray into a [formGroup] it works 🔥

This works, I think remember in Angular 6 or Angular 7, but not in newer version

@ezhupa99
Copy link

ezhupa99 commented Oct 4, 2023

This works, I think remember in Angular 6 or Angular 7, but not in newer version

inspired by #30264 (comment)

stackblitz example working on Angular 16:

@Eliseo-angular
Copy link

stackblitz example working on Angular 16:

Amazing!! It's looks like Angular guys solved!! (I'm pretty sure that not worked before, but it's possible I was wrong all this long time). Thanks @ezhupa99
BTW it's unnecessary enclosed all in a <form [formGroup]=".."> (almost in Angular 16.2)

@kremerd
Copy link

kremerd commented Oct 5, 2023

Just to clarify: The behavior here has not changed. You were always able to iterate over the controls of a FormArray and - assuming these controls are FormGroups - bind them to formGroup directives.

This feature request is about a directive that consumes FormArrays - to be consistent with FormGroups, to get the right aggregates for validity and such, and potentially to access child controls more naturally.

<form [formGroup]="formArray">

Passing a FormArray to the formGroup directive helps with some of these problems. But it also throws compiler errors since the types don't match. As I said earlier you can get around that by casting to any. Particularly with typed forms being a thing now, a typesafe solution would still be nice, though 😉🙏🏼.

@Maximaximum
Copy link

Wow, it feels quite ridiculous to see this obvious issue hanging around for 3.5 years already with no official attention from the Angular team 😕

JeanMeche added a commit to JeanMeche/angular that referenced this issue May 10, 2024
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 10, 2024
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
@JeanMeche JeanMeche linked a pull request May 20, 2024 that will close this issue
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 20, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
JeanMeche added a commit to JeanMeche/angular that referenced this issue May 30, 2024
The `FormArrayDirective` will allow to have a `FormArray` as a top-level form object.

* `NgControlStatusGroup` directive will be applied to the `FormArrayDirective`
* `NgForm` will still create a `FormGroup`

Fixes angular#30264
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: forms effort2: days feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature freq2: medium risk: medium state: Needs Design
Projects
No open projects
Feature Requests
Needs Project Proposal