Formarray does not print on my Reactive Form

Asked

Viewed 37 times

0

I’m new to Angular and I’m having a really hard time getting a Formarray into my form. Summing up what I did was:

  • Create 2 Formularies: the main (valForm) and another dynamically created (holidaysForm).
  • Copy second form values to an Array.
  • Load Array values into a Formarray.

Follows the codes:

.ts

    let group = {}
    this.holidaysForm = new FormGroup(group);

    this.valForm = fb.group({
      dentistId: [null, Validators.required],
      initialHour: [null, Validators.required],
      endHour: [null, Validators.required],
      numberOfHolidays: [null],
      appointmentsInterval: [null, Validators.required],
      year: [null, Validators.required],
      workDays: this.buildDays(),
      holidays: this.buildHolidays()
    });

  buildDays() {
    const values = this.workDays.map(v => new FormControl(false));    
    return this.fb.array(values);
  }

  buildHolidays() {
    if (typeof this.valForm !== 'undefined') {
      let teste = Object.assign({}, this.holidaysForm.value);
      this.holidays = new Array();
      Object.values(teste).forEach((v) => {
        this.holidays.push(v);
      })
      console.log(this.holidays);
      const values = this.holidays.map((v)=> new FormControl(v));      
      return this.fb.array(values);
    }
  }

  dynamicForm(val) {
    if (val > 365) {
      val = 365;
      this.valForm.patchValue({
        numberOfHolidays: 365
      })
    }
    const val_plus_one = parseInt(val) + 1
    let i: number = val_plus_one;
    if (i < this.oldNumber) {
      do {
        this.holidaysForm.get('holiday' + i.toString()).setValue(null);
        this.holidaysForm.removeControl('holiday' + i.toString());
        i++
      } while (i <= this.oldNumber)
    }
    const numbers = Array(val).fill(val, 0, 365).map((_, idx) => idx + 1)
    numbers.forEach((num) => {
      const fc = new FormControl('', FormValidations.Calendar(this.valForm.get('year').value));
      this.holidaysForm.addControl('holiday' + num.toString(), fc)
    })
    this.numberOfHolidays = numbers;
    this.oldNumber = val;
  }

.html

                        <div class="col-md-4 mda-form-group">
                            <input class="mda-form-control" type="number" min="0" max="365"
                                formControlName="numberOfHolidays"
                                (change)="dynamicForm(valForm.get('numberOfHolidays').value)" />
                            <label>Total de Feriados</label>
                        </div>
                        <div [formGroup]="holidaysForm">
                            <div class="mda-form-group" *ngFor="let num of numberOfHolidays">
                                <input class="mda-form-control" type="date" formControlName="holiday{{num}}" />
                                <label>Feriado {{num}}</label>
                                <app-error-msg [control]="holidaysForm.get('holiday'+num)" label="Feriado">
                                </app-error-msg>
                            </div>
                        </div>

In theory the code works well and without errors, the problem is that the result is always this:

Main Form

  Valores: 
 {
  "dentistId": null,
  "initialHour": null,
  "endHour": null,
  "numberOfHolidays": 3,
  "appointmentsInterval": null,
  "year": null,
  "workDays": [
    false,
    false,
    false,
    false,
    false,
    false,
    false
  ],
  "holidays": null
}

Fomulário Feriados

 {
  "holiday1": "2020-01-01",
  "holiday2": "2020-03-01",
  "holiday3": "2020-02-01"
}

Does anyone have any idea what I might be doing wrong? Grateful,

  • it was not clear what the problem was

  • I already figured out the problem. I was using holidaysForm before imputing values on it. So at the point where I was creating the object to scroll through and create my matrix, the object had no properties. So I used the code let myFormArray = this.fb.array(values);&#xA; this.valForm.addControl('holidays', myFormArray); and it worked!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.