Mohamed Wahshey
Mohamed Wahshey

Reputation: 422

Need assistance with Ionic 3 form group, cannot read property get of undefined

I just made a register form, i used to use Form-Group in Angular 5, but with Ionic 3 i get the following errors:

1- Cannot read property 'get' of undefined.

2- ERROR Error: Uncaught (in promise): Error: formGroup expects a FormGroup instance. Please pass one in.

   Example:


<div [formGroup]="myGroup">
  <input formControlName="firstName">
</div>

In your class:

this.myGroup = new FormGroup({
   firstName: new FormControl()
});

My HTML:

<ion-list>
  <form [formGroup]="register" (ngSubmit)="createUser()">
  <ion-item class="form-group" padding>
    <ion-label color="primary" floating>Name</ion-label>
    <ion-input formControlName="name" class="form-control" type="text">
    </ion-input>
  </ion-item>
  <ion-item class="form-group" padding>
    <ion-label color="primary" floating>User Name</ion-label>
    <ion-input formControlName="userName" class="form-control" type="text"> 
    </ion-input>
   </ion-item>
   <ion-item class="form-group" padding>
    <ion-label color="primary" floating>Email</ion-label>
    <ion-input formControlName="email" class="form-control" type="text"> 
    </ion-input>
   </ion-item>
   <ion-item padding class="form-group">
    <ion-label color="primary" floating>Password</ion-label>
    <ion-input formControlName="password" class="form-control" 
    type="password"></ion-input>
    </ion-item>
    <button type="submit" [disabled]="!register.valid" ion-button 
    color="primary" clear icon-start>
    <ion-icon name="contact"></ion-icon> Submit user</button>
  </form>
</ion-list>

My TS:

import { Validators, FormBuilder, FormGroup } from '@angular/forms';

register: FormGroup;
homePage = HelloIonicPage;
constructor(public navCtrl: NavController, public navParams: NavParams, 
private create: UsersData, private formBuilder: FormBuilder) {
}
ionViewDidLoad() {
this.InitUserForm();
console.log('ionViewDidLoad LoginPage');
}
InitUserForm() {
let EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)| 
(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0- 
9]+\.)+[a-zA-Z]{2,}))$/;
this.register = this.formBuilder.group({
  id: [0],
  name: ['', [Validators.required]],
  userName: ['', [Validators.required]],
  email: ['', [Validators.required, Validators.pattern(EMAIL_REGEXP)]],
  password: ['', [Validators.required, Validators.minLength(6)]]
})
}
createUser() {
this.create.createUsers(this.register.value);
}

What i have tried so far:

1- Tried switching from ion-input to normal input tag.

2- Tried adding FormsModule to app module.

3- I double checked the documentation of Ionic Forms, and did everything they said.

I need to know where is the issue, is it because i didn't add ngModel? i never use it when i use formGroup with Angular 5.

Upvotes: 1

Views: 1236

Answers (1)

Utpaul
Utpaul

Reputation: 2007

You need to change .ts file in following pattern

Importing this following package:

import { Validators, FormBuilder, FormGroup,FormControl } from '@angular/forms';

and then change inside the class

private register: FormGroup;
constructor(public navCtrl: NavController, public navParams: NavParams, 
private create: UsersData, private formBuilder: FormBuilder) {
   this.InitUserForm();
}

  InitUserForm() {

    this.register = this.formBuilder.group({
      id: [0],
      name: ['', [Validators.required]],
      userName: ['', [Validators.required]],
      email: ['', [Validators.required, this.emailValidator.bind(this)]],
      password: ['', [Validators.required, Validators.minLength(6)]]
    })
  }

  createUser() {
    console.log(this.register.value);
  this.create.createUsers(this.register.value);
}

  emailValidator(control: FormControl): {[s: string]: boolean} {
    if (!control.value.match("^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$")) {
      return {invalidEmail: true};
    }
  }

My browser look

Upvotes: 1

Related Questions