2

I cannot figure out how to patch values on an Angular form built with FormBuilder. It seems like it should be fairly obvious, but I just can't seem to get it to work. I am building a form with:

buildForm() {
const controlArray = this.redirectPortsForm.get('redirectPortsArray') as FormArray;

Object.keys(this.ports).forEach((i) => {
  controlArray.push(
    this.formBuilder.group({
      //port_entry: new FormControl({ value: this.ports[i].redirect_text, disabled: false })
      port_entry: new FormControl({ value: "", disabled: false })
    })
  )
})

console.log(controlArray.controls)
}

I've commented out a line for testing by intentionally setting it to nothing so it will be obvious when I successfully set it. I have a loop where I would like to set the value. Here is a simplified version:

const controlArray = this.redirectPortsForm.get('redirectPortsArray') as FormArray;
for (const [i, port] of ports.entries()) {
  controlArray.controls[i].patchValue({value: i.toString()});
}

I have tried many variations of this, but this is the latest. Is there something more to it? Am I missing something obvious? That code throws no errors, but also doesn't patch the values.

6
  • 1
    Shouldn't there be { port_entry: i.toString() }? Commented Jun 11, 2020 at 7:32
  • 1
    @RachidO I'd say FormArray can contain AbstractControl instances: FormControl, FormGroup, FormArray. So, you're not limited to FormControls. Commented Jun 11, 2020 at 7:33
  • My understanding was the same as @AndreiGătej's - FormArray contains AbstractControl which includes FormGroup. Now, I'm pretty new so I'm still wrapping my head around all this and FormGroup may not be necessary, but the docs seemed to indicate FormGroup was fair game. AndreiGătej, to answer your question, probably? What would the syntax be though? That's the part that's getting me. Commented Jun 11, 2020 at 13:48
  • 1
    Try to replace the ‘patchValue’s argument with what I suggested 3 comments above. Also, I’d recommend checking out this article on forms. Commented Jun 11, 2020 at 14:31
  • @AndreiGătej ohhhhhhh that's obvious now. I had spent so much time looking at this code set I forgot I had changed it to that for StackOverflow. That was it. bahahaha well, before looking at the problem again I spent a bunch of time reading about forms again so I guess I learned something 😂. Thanks! Do you want to submit that as an answer and I'll mark it as correct? Commented Jun 11, 2020 at 14:58

1 Answer 1

1

Because your FormGroup has this structure { port_entry: new FormControl(...) }, the patchValue's argument should be { port_entry: i.toString() }.

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

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.