Hello,
I am working on a project and i working on registration page right now, and i have been creating form validation for my form, I basically set validation using Angular reactive forms feature and i put my codes below (HTML)
<ion-list class="items-middle" text-center>
<!-- use the [formGroup] directive to tell Angular that we want to use the registrationForm as the "FormGroup" for this form: -->
<form [formGroup]="registrationForm">
<ion-item>
<ion-label color="ligh-grey" floating>Full Name</ion-label>
<ion-input type="text" formControlName="userName"></ion-input>
<div style="color: red; padding-top: 0.2rem" *ngIf="registrationForm.hasError('required', 'userName')">
</div>
</ion-item>
<ion-item>
<ion-label color="ligh-grey" floating>Email</ion-label>
<ion-input type="email" formControlName="email"></ion-input>
</ion-item>
<ion-item>
<ion-label color="ligh-grey" floating>Date of Birth</ion-label>
<ion-input type="text" formControlName="dob"></ion-input>
</ion-item>
<ion-item>
<ion-label color="ligh-grey" floating>Phone Number</ion-label>
<ion-input type="number" formControlName="phone" pattern="[0-9]*"></ion-input>
</ion-item>
<ion-item>
<ion-label color="ligh-grey" floating>Address</ion-label>
<ion-input type="text" formControlName="address"></ion-input>
</ion-item>
<ion-item class="job_status">
<ion-label color="ligh-grey" floating>Job Status (Position)</ion-label>
<ion-input type="text" formControlName="jobStatus"></ion-input>
</ion-item>
<ion-item>
<ion-label>Job Sector</ion-label>
<ion-select formControlName="jobSectore">
<ion-option value="f">Government</ion-option>
<ion-option value="m">Private</ion-option>
</ion-select>
</ion-item>
<input type="checkbox" formControlName="isTosRead">
<!-- We can check if our form is valid: -->
<ion-buttons padding-top>
<button ion-button full round type="submit" [disabled]="!registrationForm.valid" (click)="regForm()">SIGNUP</button>
</ion-buttons>
</form>
<pre> {{registrationForm.value | json }}</pre>
</ion-list>
And this is my TS file
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FormGroup, FormControl, Validators } from '@angular/forms';
@IonicPage()
@Component({
selector: 'page-registration',
templateUrl: 'registration.html',
})
export class RegistrationPage {
registrationForm: FormGroup;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.registrationForm = new FormGroup({
userName: new FormControl('', [Validators.required, Validators.minLength(2), Validators.pattern('^[a-zA-Z]+$')]),
email: new FormControl('', [Validators.required, Validators.email, Validators.minLength(4)]),
dob: new FormControl('', [Validators.required, Validators.minLength(4)]),
phone: new FormControl('', [Validators.required, Validators.minLength(9), Validators.maxLength(10)]),
address: new FormControl('', [Validators.required, Validators.minLength(9), Validators.maxLength(255)]),
jobStatus: new FormControl('', [Validators.required, Validators.minLength(2), Validators.maxLength(50)]),
jobSectore: new FormControl('', [Validators.required]),
isTosRead: new FormControl(false, [Validators.requiredTrue])
})
}
}
This form show color validation when input is write or wrong, but i need show readable error message to the user i need to know how can i archive it. I used this code <div style="color: red; padding-top: 0.2rem" *ngIf="registrationForm.hasError('required', 'userName')"> </div>
to show some errors but it is not worked donāt know why.
Please help me easy way to do this?
Thank you