My registration form in angular2 is created using <form>
. Here's how it looks:
<form [formGroup]="RegisterForm3">
<ion-item>
<ion-label>Last Date:</ion-label>
<ion-input formControlName="LastDate" min="2016" type="date"></ion-input>
</ion-item>
<hr/>
<ion-item>
<ion-label>Next Date:</ion-label>
<ion-datetime displayFormat="MM/DD/YYYY" formControlName="NextDate" (OnChanges)="npay()"></ion-datetime>
</ion-item>
<span style="color:red" *ngIf="!RegisterForm3.controls.NextPayDate.valid && (!RegisterForm3.controls.NextDate.dirty || submitAttempt)"> * Next day Must Be Greater Than Today's Date </span>
<hr/>
<ion-item>
<ion-label>SecondDate:</ion-label>
<ion-datetime displayFormat="MM/DD/YYYY" formControlName="SecondDate"></ion-datetime>
</ion-item>
<hr/>
<ion-item>
<ion-label>Original Date:</ion-label>
<ion-datetime displayFormat="MM/DD/YYYY" formControlName="RequestedDate"></ion-datetime>
</ion-item>
<span style="color:red" *ngIf="!RegisterForm3.controls.RequestedDate.valid && (!RegisterForm3.controls.RequestedDate.dirty || submitAttempt)"> * RequestedDate Should Be 5 days Greater Than Today's Date </span>
</form>
In the form.ts file:
this.RegisterForm3 = formBuilder.group({
LastDate: ['', Validators.required],
NextDate:['',NextDayValidator.isValid],
SecondDate: ['',Validators.required],
RequestedDate:['',OriginalDateValidator.isValid],
});
I have also implemented two custom validator classes.
import { FormControl } from '@angular/forms';
export class OriginalDateValidator {
static isValid(control: FormControl): any {
var todaydate =new Date(Date.now() + (1000 * 60 * 60 * 24 * 10))
console.log(todaydate);
let duedate=new Date(control.value);
console.log(duedate);
console.log(duedate<todaydate);
//alert(npay);
if(!duedate){
return {
"is empty": true
};
}
if(duedate<todaydate){
return {
"Day must 5day greater then todaydate": true
};
}
return null;
}
}
A challenge I'm facing is that RequestedDate must be equal to either NextDate or SecondDate. I need to test this condition in my OriginalDateValidator class, but can only access the RequestedDate field in my validators. Is there a way to access NextDate and SecondDate in my validator's class as well? Any suggestions?