Form validation error in ionic 2. "Can't resolve all parameters for FormGroup: (?, ?, ?)"


#1

I am trying to use forms in ionic 2. And wrote this very basic application, however, keep getting the following error: “Can’t resolve all parameters for FormGroup: (?, ?, ?)”

ts file:

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'page-register',
  templateUrl: 'register.html'
})
export class RegisterPage {

  private myData: any;
          myForm: FormGroup;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private builder: FormBuilder) {

                this.myForm = builder.group({
                 'subject': '',
                 'message': ''
                })

              }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegisterPage');
  }

  onSubmit(formData) {
   if(formData.valid) {
     console.log(formData.value);
     this.myData = formData.value;
   }
 }

html file:

<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">`
          <ion-item>
              <ion-label floating>Subject</ion-label>
              <ion-input type="text" formControlName="subject" name="subject"></ion-input>
          </ion-item>

      <ion-item>
          <ion-label floating>Message</ion-label>
          <ion-textarea type="text" formControlName="message" name="message"></ion-textarea>
      </ion-item>


      <button>
        <ion-icon name="add"></ion-icon>Add
      </button>

  </form>

I am new to ionic, has anyone dealt with this kind of issue?


#2

As far as i know a formcontrol can receive up to 3 parameters inside an array for the validators, so you need to wrap you ’ ’ inside a []. Try:

this.myForm = builder.group({
             'subject': [''],
             'message': ['']
            })

#3

Still getting the same error!


#4

Solved it, I was importing modules more than once!