0

This is the structure of my form:

this.formData = new FormGroup({
  selectedAnimal: new FormArray([], [Validators.required]),
  selectedTransport: new FormArray([], [Validators.required]),
  roadName: new FormControl({ disabled: true, value: null }, Validators.required),
  roadZip: new FormControl({ disabled: true, value: null }, Validators.required),
  planeName: new FormControl({ disabled: true, value: null }, Validators.required),
  planeZip: new FormControl({ disabled: true, value: null }, Validators.required)
});

Corresponding HTML

<form [formGroup]="formData" (ngSubmit)="onSubmit()">
  <div class="animal-checkbox-group" formArrayName="selectedAnimal">
    <!-- multiple checkbox options, selecting one is mandatory -->
  </div>

  <div class="animal-checkbox-group" formArrayName="selectedTransport">
    <!-- multiple checkbox options, selecting one is mandatory -->
    <div class="if-checkbox-1-selected">
      <!-- conditional checkbox: if checkbox is selected -> new new form controls -> they should be defined for successful validation -->
      <input type="text" formControlName="roadName">
    </div>
  </div>
</form>

ERROR:

ERROR Error: Cannot find control with path: 'selectedTransport -> roadName'

Aakash Goplani
  • 1,150
  • 1
  • 19
  • 36
  • That because `formControlName="roadName"` is under `selectedTransport` formArray. You either have to change the structure of your html to make `formControlName="roadName` to be under `[formGroup]="formData"` or push controls to the formArray and loop them in template –  May 08 '21 at 17:09

1 Answers1

3

In your case control is rendered inside the formarray abstract control so You must need to provide form group to all your controls name as mentioned below:

<div [formGroup]="formData">
    Road Name: <input type="text" formControlName="roadName">
</div>

Here is working code : stackblitz

Devang Patel
  • 1,795
  • 4
  • 21