question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ControlValueAccessor with FormArray

See original GitHub issue

πŸš€ feature request

Relevant Package

This feature request is for @angular/forms

Description

I have a child component which deals with the `FormArray` of input controls. I want to have a formcontrol over the child component.

I want to have a custom-child component say name-list. The parent FormControl will send FormArray to name-list. Then in NameListComponent will use FormArray and manipulate according to need just like FormControl do using ControlValueAccessor.

I have searched for similar functionality, I just come across this question without legitimate answer

Example

<h1>Child</h1>
<div formArrayName="names" *ngFor="let name of names.controls; let i = index;">
 <div [formGroupName]="i" >
  <input formControlName="firstName">
  <input formControlName="lastName">
 </div>
</div>

Intention is to bind parent form with the array of input control in the child component. Also child form will have validation checks.

Describe the solution you’d like

Need a similar `interface ControlValueAccessor` like `ArrayAccessor

Describe alternatives you’ve considered

I have tried to send `formArrayName` to custom component, but it seems `writeValue(obj: any)` never called for `formArrayName`, it only works for `formControlName`.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
lazarljubenoviccommented, Sep 8, 2019

I don’t think this is a bug or missing feature; I believe you’re trying to misuse form controls.

If your child component has an array of controls, then it’s not a control. It would be an anti-pattern to have form controls inside something that’s a form control itself: this is what form groups (FormGroup) and form arrays (FormArray) have been created for. Your component is simply a component that should somehow be made aware that its controls are part of an externally created form. Not a control itself.

I haven’t checked out ngx-sub-form which @maxime1992 suggested above, but I guess it’s a nice wrapper around a technique I’ve described in this StackOverflow answer: https://stackoverflow.com/a/46025197/2131286.

Basically, if you put a formGroup, formGroupName, formArray or formArrayName on a component, then that component can pick up something called Control Container through injection. Inject ControlContainer in the reusabile component, and ControlContainer#control will give you the control, decided by formArrayName or similar.

1reaction
jorrollcommented, Aug 6, 2019

This example, combined with the code from my previous comment, does what you want it to.

<div>
  <form [formGroup]="topForm">
    <input formControlName="f1" />
    <div class="mt-3" *ngFor="let control of topForm.get('anArray')!.controls">
      <input [formControl]="control.get('field1')" />
      <input [formControl]="control.get('field2')" />
      <app-child [control]="control.get('child')"></app-child>
    </div>
  </form>
</div>

I’ll point out the obvious, you still have complete access to topForm from the parent component.

Like I have to handle lots of logic in parent component (which should be not needed manually if we have something like ArrayValueAccessor).

I’m not sure what you mean by this. What do you think a hypothetical ArrayValueAccessor would do for you?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ControlValueAccessor with FormArray in Angular 2
I want to have a formcontrol over the child component. I am passing the array of json object, what would be the correct...
Read more >
Angular Nested Reactive Form Control Value Accessor
import { FormGroup, FormArray, FormControl,. Validators, FormBuilder, AbstractControl }. from '@angular/forms';. @Component({. selector: 'my-app',.
Read more >
ControlValueAccessor with FormArray in Angular 2 - iTecNote
I have a child component which deals with the array of input controls. I want to have a formcontrol over the child component....
Read more >
Don't reinvent the wheel when implementing ... - Medium
Reuse already-implemented controlValueAccessor ... controlContainer.control is the parent FormGroup (or FormArray) instance. */
Read more >
Creating custom form controls using Control Value Accessor ...
How to create custom form controls in Angular using ControlValueAccessor? We can create custom form components and connect them to eitherΒ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found