Search code examples
angulartypescriptionic3angular-reactive-formsion-select

Ionic3/Angular Error: Must supply a value for form control with name: 'jobStatus'


I have a Modal Form with the following code in the template markup

  <form [formGroup]="modalFG">
  ...
  <ion-item *ngIf="isChangeStatus()">
      <ion-select formControlName="jobStatus"
                  (ionChange)="jobStatusChangeHandler($event)">
        <ion-option value=4>Success</ion-option> 
        <ion-option value=5>Failure</ion-option>
        <ion-option value=6>Terminated</ion-option> 
        <ion-option value=8>Inactive</ion-option> 
      </ion-select>
  </ion-item>
  </form>

In the Typescript I've specified a value, but I can't get past this error.

Error: Must supply a value for form control with name: 'jobStatus'.

Can someone tell me what I'm doing wrong?

...
private modalFG:FormGroup;
private jobStatus:number;
constructor(
  private navParams:NavParams,
  private view:ViewController,
  private fb: FormBuilder,
  ...
) {
    this.changeStatus = false;
    this.modalFG = fb.group({
      comment: ['', Validators.required],
      jobStatus: this.jobStatus
    });
}
private ionViewWillLoad():void {
    const data = this.navParams.get('data');
    this.modalFG.setValue({'jobStatus': this.jobStatus});
}

private jobStatusChangeHandler(ev:any) {
    console.log(ev);
}

private isChangeStatus():boolean {
    return this.changeStatus;
}

I also have button handler for submit with this:

this.exitData.jobStatus = this.modalFG.get('jobStatus').value;
this.view.dismiss(this.exitData);

This is the full error message:

Error: Must supply a value for form control with name: 'jobStatus'.
    at http://localhost:8100/build/vendor.js:22102:23
    at http://localhost:8100/build/vendor.js:22026:66
    at Array.forEach (<anonymous>)
    at FormGroup._forEachChild (http://localhost:8100/build/vendor.js:22026:36)
    at FormGroup._checkAllValuesPresent (http://localhost:8100/build/vendor.js:22100:14)
    at FormGroup.setValue (http://localhost:8100/build/vendor.js:21907:14)
    at ModalPage.webpackJsonp.124.ModalPage.ionViewWillLoad (http://localhost:8100/build/main.js:648:22)
    at ModalImpl.ViewController._lifecycle (http://localhost:8100/build/vendor.js:17471:33)
    at ModalImpl.ViewController._willLoad (http://localhost:8100/build/vendor.js:17331:14)
    at OverlayPortal.NavControllerBase._willLoad (http://localhost:8100/build/vendor.js:44112:18)

Since it says ionViewWillLoad in the call stack, the error must be something in that section AFAIK!


Solution

  • Right... I had a form that used to only have a single form control, called 'comment'.

    When I added a second, conditional form element, the syntax you need to use changes for setValue...

    I needed to modify it from something like this:

      this.modalFG.setValue({'comment': data.comment});
      this.modalFG.setValue({'jobStatus': 0});
    

    to:

      this.modalFG.controls['jobStatus'].setValue(0);
      this.modalFG.controls['comment'].setValue(data.comment);
    

    because I now had two fields on the form...

    Then everything fell into place and the misleading message about not having supplied form field disappeared.

    I hate the syntax of Angular here, changing it's functioning behaviour when you go from 1 to n values!