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'

1
  • 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 Commented May 8, 2021 at 17:09

1 Answer 1

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

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.