3

Trying to create html for reactive forms, but i always get error

this is my form

 private buildForm(): void {
    this.formOperational = this.formBuilder.group({
      comment: [this.expertOperational.comment],
      workingDays: new FormArray([])
    });
  }

  private createWorkignDays(): void {
    this.operational.workingDay.forEach((day: WorkingDay) => {
      this.workHoursControls.push(
        this.formBuilder.group({
          weekDay: [day],
          operationalAM: this.formBuilder.group({
            fixed: [day.operationalAM.fixed],
            startTime: [day.operationalAM.startTime],
            endTime: [day.operationalAM.endTime]
          }),
          operationalPM: this.formBuilder.group({
            fixed: [day.operationalAM.fixed],
            startTime: [day.operationalAM.startTime],
            endTime: [day.operationalAM.endTime]
          })
        })
      );
    });
  }

When i HTML i try something like this

<form [formGroup]="formOperational">
    <div class="row pb-1" *ngFor="let day of formOperational.get('workingDays').controls; let i = index;">
        <div class="col-lg-2 col-sm-2 col-md-2 justify-content-center align-self-center">
            {{'Shared.WeekDay.' + day.weekDay | translate }}
        </div>
        <div class="row" [formGroup]="i">
            <div class="text-center col">
                <input type="checkbox" formControlName="fixed">
            </div>
        </div>
        <div class="col">
            <input class="form-control text-center width-70" formControlName="startTime" />
        </div>
        <div class="col">
            <input class="form-control text-center width-70" formControlName="endTime" />
        </div>
    </div>
    <div class="form-group">
        <textarea class="form-control rounded-0" formControlName="comment"
            rows="11">{{expertOperational.comment}}</textarea>
    </div>
</form>

I got error

ERROR Error: formGroup expects a FormGroup instance. Please pass one in.

       Example:


    <div [formGroup]="myGroup">
      <input formControlName="firstName">
    </div>

    In your class:

    this.myGroup = new FormGroup({
       firstName: new FormControl()
    });
    at Function.push../node_modules/@angular/forms/fesm5/forms.js.ReactiveErrors.missingFormException (forms.js:1443)
    at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective._checkFormPresent (forms.js:5414)
    at FormGroupDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormGroupDirective.ngOnChanges (forms.js:5237)
    at checkAndUpdateDirectiveInline (core.js:22095)
    at checkAndUpdateNodeInline (core.js:23363)
    at checkAndUpdateNode (core.js:23325)
    at debugCheckAndUpdateNode (core.js:23959)
    at debugCheckDirectivesFn (core.js:23919)
    at Object.eval [as updateDirectives] (ExpertOperationalComponent.html:6)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)

Does anybody knows where i am doign wrong? Thanks

8
  • Where did you call the buildForm function()? Commented Dec 23, 2019 at 11:05
  • when are you creating Forms Commented Dec 23, 2019 at 11:07
  • On ngInit, but i think problem i that i dont bind proper html, i dont know how? Commented Dec 23, 2019 at 11:07
  • Put *ngIf="formOperational" then it will bind only if it exist on form Commented Dec 23, 2019 at 11:07
  • I think my html is not good :( Commented Dec 23, 2019 at 11:08

2 Answers 2

2

You said that your HTML is not good. Are you facing an error there? Try this
Remove

*ngFor="let day of formOperational.get('workingDays').controls; let i = index;"

Add
set up a getter

get controls() {
  return (this.formOperational.get('workingDays') as FormArray).controls;
}

and then in the template

*ngFor="let day of controls; let i = index"
Sign up to request clarification or add additional context in comments.

Comments

0

you shoulde declare a FormGroup like formOperational: FormGroup;. try this in your comonent.ts,:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup} from '@angular/forms';


@Component({
  selector: 'app-example',
  templateUrl: './app-example.component.html',
  styleUrls: ['./app-example.component.scss']
})

export class AppExampleComponent implements OnInit {

  formOperational: FormGroup;

    constructor(
    private _formBuilder: FormBuilder,
  ) {}

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.