0

Error on create form array

core.js:6260 ERROR TypeError: control.registerOnChange is not a function at setUpModelChangePipeline (forms.js:3528) at setUpControl (forms.js:3405) at FormControlDirective.ngOnChanges (forms.js:7382) at FormControlDirective.wrapOnChangesHook_inPreviousChangesStorage (core.js:26975) at callHook (core.js:4762) at callHooks (core.js:4722) at executeInitAndCheckHooks (core.js:4662) at selectIndexInternal (core.js:9724) at Module.ɵɵadvance (core.js:9685) at BuildingPartsModalComponent_div_6_Template (test.component.html:37)

Here is my code Html

   <form [formGroup]="demoForm">
        <div formArrayName="demoArray" *ngFor="let arrayItem of arrayItems; let i=index">
          <input [id]="arrayItem.parts" type="checkbox" [formControl]="arrayItems[i]">
          <label [for]="arrayItem.volume" class="array-item-title">
            {{arrayItem.name}}</label>
          <div (click)="removeItem(i)">remove item</div>
        </div>
        <button (click)="submit()">Go</button>
      </form>
      <div (click)="addItem()">Add new</div>

TS

  demoForm: FormGroup;

  arrayItems: {
    parts: number;
    name: string;
    volume: string;
  }[];



ngOnInit(): void {
    this.arrayItems = [{
      parts: 11,
      name: 'Naslov',
      volume: '120'
    }];
    this.demoForm = this.formBuilder.group({
      demoArray: new FormArray([])
    });
  }

  get demoArray() {
    return this.demoForm.get('demoArray') as FormArray;
  }

  addItem() {
    const item = {
      parts: 11,
      name: 'Naslov2',
      volume: '120'
    };
    this.arrayItems.push(item);
    this.demoArray.push(this.formBuilder.control(false));
  }

  removeItem(index) {
    this.arrayItems.splice(index, 1);
    this.demoArray.removeAt(this.demoArray.length - 1);
  }
  submit() {
    console.log(this.demoForm.value);
  }

I made it much simpler that everybody can understand and to help me find the problem, thanks

7
  • Please read the docs, your .html don't work to control a FormArray. If you want you can also check this SO: stackoverflow.com/questions/63541082/… to know the diference when we using a FormArray of FormControls or a FormArray of FormGroup Commented Aug 27, 2020 at 7:15
  • @Eliseo what do you mean .html doesn't work? what makes you thinks so? Commented Aug 27, 2020 at 7:28
  • @RafiHenig, in your answer it's NOT [formArrayName]="i", it's [formGroupName]="i" Commented Aug 27, 2020 at 7:30
  • @Eliseo why [formGroupName]="i"?, the array of is of type FormControl rather than FormGroup Commented Aug 27, 2020 at 7:30
  • @RafiHenig, sorry, [formControlName]="i" Commented Aug 27, 2020 at 7:31

2 Answers 2

1

You can use addControl('control_name', new FormControl('')) method to add a new control or form group dynamically to your form like below:

this.form = new FormGroup({
    name: new FormControl('', [Validators.required]);
});

To add a FormGroup dynamically with validation use:

this.form.addControl('address', new FormGroup({
    city: new FormControl('', [Validators.required]),
    state: new FormControl('', [Validators.required]),
    country: new FormControl('', [Validators.required])
}));

To add a FormArray dynamically with validation use:

this.form.addControl('address', new FormArray([
     new FormGroup({
        city: new FormControl('', [Validators.required]),
        state: new FormControl('', [Validators.required]),
        country: new FormControl('', [Validators.required])
    })
]));

Your HTML template for FormArray example

<form [formGroup]="form">
    <div formArrayName="address">
        <ng-container *ngFor="let group of form.address.controls; let i = index" [formGroupName]="i">
            <input type="text" formControlName="city" placeholder="city" />
            <input type="text" formControlName="state" placeholder="state" />
            <input type="text" formControlName="country" placeholder="Country" />
        </ng-container>
    </div>
</form>

Visit here to get more clarity to add/remove controls dynamically in Reactive Forms.

Sign up to request clarification or add additional context in comments.

2 Comments

How does it answer the question?
@RafiHenig Your question is add dynamic FormArray with validation, so i answered that you can add any control dynamically using addControl method. you can add FormArray with Validation like this: this.form.addControl('address', new FormArray([new FormGroup({ city: new FormControl('', [Validators.required]), state: new FormControl('', [Validators.required]), country: new FormControl('', [Validators.required]) })]));
0

Replace[formControl]="arrayItems[i]" with [formControlName]="i", as demonstrated below:

<form [formGroup]="demoForm">
  <div formArrayName="demoArray">
    <input *ngFor="let arrayItem of arrayItems; let i = index" [formControlName]="i">
  </div>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.