I am new to ionic2 making an app.when adding form validation facing an error “formGroup expects a FormGroup instance. Please pass one in”
signup.html
Signup
<ion-item>
<ion-label fixed>Password</ion-label>
<ion-input type="password" name="password" [(ngModel)]="registerCredentials.password" requried></ion-input>
</ion-item>
</ion-list>
<div padding>
<button ion-button block (click)="register(registerCredentials)">Sign Up</button>
</div>
signup.ts
import { Component } from ‘@angular/core’;
import { AuthService } from ‘…/…/providers/auth-service’;
import ‘rxjs/add/operator/debounceTime’;
import { NavController, AlertController, ToastController } from ‘ionic-angular’;
import { Http } from ‘@angular/http’;
import { LoginPage } from ‘…/login/login’;
import { Validators, FormGroup, FormControl} from ‘@angular/forms’;
@Component({
templateUrl: ‘signup.html’
})
export class SignupPage {
sampleForm: FormGroup;
createSuccess = false;
registerCredentials:any = {email: ‘’, password: ‘’};
constructor(private nav: NavController, private alertCtrl: AlertController, private auth: AuthService,private toastCtrl: ToastController, public http : Http) {}
ionViewDidLoad() {
this.sampleForm = new FormGroup({
email: new FormControl('', Validators.compose([
Validators.required,
Validators.pattern('^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$')
])),
password: new FormControl('', Validators.compose([
Validators.minLength(5),
Validators.required,
Validators.pattern('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$')
]))
})
this.sampleForm.valueChanges
.debounceTime(400)
.subscribe(data => this.onValueChanged(data));
}
onValueChanged(data?: any) {
if (!this.sampleForm) { return; }
const form = this.sampleForm;
for (const field in this.formErrors) {
// clear previous error message
this.formErrors[field] = [];
this.sampleForm[field] = ‘’;
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field].push(messages[key]);
}
}
}
}
formErrors = {
‘email’: [],
‘password’: []
};
validationMessages = {
‘email’: {
‘required’: ‘Email is required’,
‘pattern’: ‘Enter a valid email.’
},
‘password’: {
‘required’: ‘Password is required’,
‘minlength’: ‘Password must be at least 5 characters long.’,
‘pattern’: ‘Your password must contain at least one uppercase, one lowercase, and one number.’
}
};
register(registerCredentials){
// console.log('hello');
// this.http.post('http://10.20.1.166:3000/users/register').map(res => res.json()).subscribe(response => {console.log('success')});
this.auth.register(registerCredentials).subscribe(response => {
if(response.resStatus == 'success') {
this.createSuccess = true;
// this.showPopup("Success", "Account created.");
// console.log('response'+response.resStatus);
let toast = this.toastCtrl.create({
message: 'Account created successfully',
duration: 3000,
position: 'top'
});
toast.present();
this.nav.setRoot(LoginPage);
} else {
}
})
}
}
cheers!