0

I have a formgroup with the following structure:

this.myForm = this.formBuilder.group({
  firstGroup: this._fb.group({
     isEnabled: [false, Validators.required],
     firstArray: this._fb.array([])
  }),
  secondGroup: this._fb.group({
     isEnabled: [false, Validators.required],
     secondArray: this._fb.array([])
  }),
  thirdGroup: this._fb.group({
     isEnabled: [false, Validators.required],
     thirdArray: this._fb.array([])
  }),
  firstName: ['', Validators.required],
  lastName: ['', Validators.required]
})

I have data coming from the backend which I want to populate in the nested formArrays namely firstArray, secondArray, thirdArray

The current approach I have tried is the following:

this.myForm.get('firstGroup.firstArray').setValue(data.firstArray)
this.myForm.get('firstGroup.isEnabled').setValue(data.isEnabled)

But doing this gives the following error:

value.forEach is not a function

I have previously worked with dynamic forms but they were not nested like this. Any help on where I am wrong with this will be greatly appreciated.

createItem() {
  return this.formBuiler.group({
      temp1: [{
          key1: null,
          key2: null,
      }],
      temp2: [{
          key3: null,
          key4: null,
      }],
  })
}

The formarray is an array of objects like given above (this method is called first item to create item for formArray)

2
  • Form.get([‘temp1’, ‘key1’]) Commented Jun 14, 2022 at 17:19
  • I am able to map the coming data to my form group. When I console log the form group it displays the values however they are still not being reflected in the template/html. Commented Jun 15, 2022 at 6:29

0

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.