Hello.
Having an issue with validating input fields in Ionic 4, FormBuilder.
I’m using as an example from Josh Morony book - Building Mobile Apps - Lesson 9.
I use FormBuilder to create a FormGroup that has 2 fields in it that map to input fields in the page template. I also add Validators.required to each field in the .ts file.
The data binding portion is working, as I see values typed into the fields appear in log console output. However, no validation occurs… If I leave fields blank, and click button for next page, it just goes on to next page…
So, not sure what I’m missing, but having trouble finding examples of this in Ionic 4.
Code snippets below. Any help appreciated.
home.page.ts:
import {Component} from '@angular/core';
import {Router} from '@angular/router';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
public loginForm: FormGroup;
constructor(public router: Router, public formBuilder: FormBuilder) {
this.loginForm = formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
}
onSignIn() {
console.log('what is goin on');
console.log(this.loginForm.value);
this.router.navigateByUrl('/referring-physician');
}
}
home.page.html (portion):
<form [formGroup]="loginForm">
<ion-list>
<ion-item>
<ion-label position="stacked">Username</ion-label>
<ion-input formControlName="username" type="text">
</ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Password</ion-label>
<ion-input formControlName="password" type="text">
</ion-input>
</ion-item>
</ion-list>
<ion-row class="signin">
<ion-col>
<ion-button (click)="onSignIn()" color="primary" shape="full" expand="block">Sign In</ion-button>
</ion-col>
</ion-row>
</form>