sunnyj58
sunnyj58

Reputation: 103

Ionic form required validation

Having some trouble understanding some simple validation using ionic. Ive used AngularJS before and it was really simple to do in the HTML, however its a real pain in Ionic it seems.

The error I am getting is -

Can't resolve all parameters for FormGroup: (?, ?, ?).

I just want to make fields required before a user can continue. Here is my code -

ts.

myGroup : FormGroup;

constructor(public navCtrl: NavController, 
public navParams: NavParams,    
private formGroup: FormGroup
) {

this.myGroup = new FormGroup({
  height: new FormControl(Validators.required),
  age: new FormControl(Validators.required)
})
}

html.

form [formGroup]="myGroup">
<ion-input formControlName="age" type="number" name="age">Age</ion-input>
<ion-input formControlName="height" type="number"  name="height">Height</ion-input>
<button type="submit" ion-button (click)="CalculateBMR()">Submit</button>
</form>

Edit - Noticed the error seems to come from -

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

If I remove all the above code, I get no errors. However when I add this to the app.module.ts file, I get the above error.

Upvotes: 0

Views: 687

Answers (1)

AndrWeisR
AndrWeisR

Reputation: 1226

Remove private formGroup: FormGroup from your constructor. You don't need to inject it, as you are creating a new instance of it explicitly.

Upvotes: 1

Related Questions