0
votes

I am trying to create new FormArray, but i only have one member in array right now, when creating new FromBuilder i have like this

public myAccountForm: FormGroup;

  ngOnInit() {
    this.myAccountForm = this._fb.group({
      FullName: ['', Validators.compose([
        Validators.required
      ])],
      Email: ['', Validators.compose([
        Validators.required
      ])],
      Owner: this._fb.group({
        IsOwner: [false],
        Vehicles: this._fb.array([{
          Model: '',
          Vin: '',
          YearManufacture: '',
          RegistrationPlate: '',
          LastServiceDate: ''
        }])
      })
    });

And HTML

<form class="list-form" role="form" (ngSubmit)="onSubmitMyAccount(myAccountForm)" [formGroup]="myAccountForm">
    <ion-item>
        <ion-label color="primary" stacked>
            {{"Full Name" | translate }}:
        </ion-label>
        <ion-input type="text" formControlName="FullName" value="{{userFullName}}"></ion-input>
    </ion-item>
    <p ion-text color="danger" class="text-1x has-error" *ngIf="myAccountForm.get('FullName').touched && myAccountForm.get('FullName').hasError('required')">{{"This field is required" | translate }}</p>
    <ion-item>
        <ion-label color="primary" stacked>
            {{"Email Address" | translate }}:
        </ion-label>
        <ion-input type="email" formControlName="Email" disabled value="{{userEmail}}"></ion-input>
    </ion-item>
    <p ion-text color="danger" class="text-1x has-error" *ngIf="myAccountForm.get('Email').touched && myAccountForm.get('Email').hasError('required')">{{"This field is required" | translate }}</p>
    <div formGroupName="Owner" class="m-t-15">
        <p>{{"Are you owner?" | translate }}</p>
        <ion-item radio-group margin-top class="no-border">
            <ion-label>{{"Yes" | translate }}</ion-label>
            <ion-checkbox color="white" checked="true" value="true" formControlName="IsOwner" (ionChange)="getIsOwner($event)"></ion-checkbox>
        </ion-item>
        <div formArrayName="Vehicles" *ngIf="isOwner">

            <ion-list [formGroupName]="0">

                <ion-item>
                    <ion-label color="primary" stacked>
                        {{"Model" | translate }}:
                    </ion-label>
                    <ion-input type="text" formControlName="Model" value="{{userModel}}"></ion-input>
                </ion-item>
                <ion-item>
                    <ion-label color="primary" stacked>
                        {{"VIN Number" | translate }}:
                    </ion-label>
                    <ion-input type="text" formControlName="Vin"></ion-input>
                </ion-item>
                <ion-item>
                    <ion-label color="primary" stacked>
                        {{"Year Manufactured" | translate }}:
                    </ion-label>
                    <ion-datetime displayFormat="YYYY" formControlName="YearManufacture"></ion-datetime>
                </ion-item>
                <ion-item>
                    <ion-label color="primary" stacked>
                        {{"Registration Plate" | translate }}:
                    </ion-label>
                    <ion-input type="text" formControlName="RegistrationPlate"></ion-input>
                </ion-item>
                <ion-item>
                    <ion-label color="primary" stacked>
                        {{"Last Service Date" | translate }}:
                    </ion-label>
                    <ion-datetime displayFormat="MM/DD/YYYY" formControlName="LastServiceDate"></ion-datetime>
                </ion-item>
            </ion-list>

        </div>
    </div>
    <div class="m-t-30">
        <button ion-button round block class="m-b-10" color="primary" type="submit" [disabled]="!myAccountForm.valid">
                  {{"Edit" | translate }}
                </button>
    </div>
</form>

And i got error like this Cannot find control with path: 'Owner -> Vehicles -> 0 -> Model'

1

1 Answers

2
votes

When you create a FormArray with the builder you need to pass as parameter an Array of FormControl ( or FormGroup in your case ). But you are passing an Array of plain Object instead of it.

Change

    Vehicles: this._fb.array([{
      Model: '',
      Vin: '',
      YearManufacture: '',
      RegistrationPlate: '',
      LastServiceDate: ''
    }])

to

    Vehicles: this._fb.array([this._fb.group({
      Model: '',
      Vin: '',
      YearManufacture: '',
      RegistrationPlate: '',
      LastServiceDate: ''
    })])