Hi,
In my ionic project reactive forms are not working. Ideally when user enters a wrog value, it should display erroe message immediately but it do not. I could not figure our where the problem is and need some help.
I am using Ionic version 5.2.3 and npm version 6.9.0
Here is the code
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl, ReactiveFormsModule } from '@angular/forms';
@Component({
selector: 'app-register',
templateUrl: './register.page.html',
styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {
fg: FormGroup;
constructor(public fb: FormBuilder) { }
ngOnInit() {
this.fg = this.fb.group({
emailControl: new FormControl('', Validators.required),
passwordControl: new FormControl('', Validators.required),
confirmControl: new FormControl('', Validators.required),
mobileControl: new FormControl('', Validators.required),
add1Control: new FormControl(''),
add2COntrol: new FormControl(''),
postcodeControl: new FormControl(''),
stateControl: new FormControl('')
});
}
onSubmit(values) {
console.log(values);
}
}
The UI code is here
<ion-content>
<form [attr.formGroup]="fg" (ngSubmit)="onSubmit(fg.value)">
<ion-list lines="full" class="ion-no-margin ion-no-padding">
<ion-item>
<ion-label position="stacked">Email Address*</ion-label>
<p>
<!-- <ion-input type = "email" placeholder="Register - Enter valid email address"></ion-input> -->
<ion-input type = "email" attr.formControlName="emailControl" required></ion-input>
</p>
</ion-item>
<ion-item>
<div *ngIf="fg.get('emailControl').invalid">
<!-- <div > -->
<ion-label position="stacked">{{ fg.get('emailControl').invalid }} {{ fg.get('emailControl').dirty}} {{ fg.get('emailControl').touched}}</ion-label>
</div>
</ion-item>
<ion-item>
<ion-label position="stacked">Password*</ion-label>
<p>
<ion-input type = "password" attr.formArrayName="passwordControl" required placeholder="Enter password"></ion-input>
<!-- <ion-input type = "password" formArrayName="passwordControl" placeholder="Enter password"></ion-input> -->
</p>
</ion-item>
<ion-item>
<ion-label position="stacked">confirm Password*</ion-label>
<p><ion-input type = "password" attr.formControlName="confirmControl" required placeholder="Confirm password"></ion-input></p>
</ion-item>
<ion-item>
<ion-label position="stacked">Mobile Number*</ion-label>
<p><ion-input type = "number" attr.formControlName="mobileControl" placeholder="Enter mobile number"></ion-input></p>
</ion-item>
<ion-item>
<ion-label position="stacked">Address</ion-label>
<p><ion-input type = "text" attr.formControlName="add1Control" placeholder="Address Lin 1"></ion-input>
<ion-input type = "text" attr.formControlName="add2Control" placeholder="Address Lin 2"></ion-input>
<ion-input type = "text" attr.formControlName="postcodeControl" placeholder="Postcode"></ion-input>
<ion-input type = "text" attr.formControlName="stateControl" placeholder="State"></ion-input></p>
</ion-item>
<ion-item>
<ion-button type="submit" color="success" expand="full">Register</ion-button>
<p></p>
</ion-item>
<ion-item>
<ion-label position="stacked">Message here</ion-label>
</ion-item>
</ion-list>
</form>
</ion-content>
Module.ts is as below
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { IonicModule } from '@ionic/angular';
import { RegisterPage } from './register.page';
const routes: Routes = [
{
path: '',
component: RegisterPage
}
];
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
IonicModule,
RouterModule.forChild(routes)
],
declarations: [RegisterPage]
})
export class RegisterPageModule {}
I have seen many online documentation and examples but could not figure out where the problem is. ANy help will be highly appreciated.