I am attempting to do date validation in Angular2 with FormControls. I have a start and end date. I need to validate that the start date is before the end date. Here is how I am editing the validators on the controls when their values change:
this.editForm.get('startDate').valueChanges.debounceTime(100).subscribe(
(startDate: any) => {
if (startDate != null) {
this.editForm.get('startDate').setValidators([FormValidators.validateDate, FormValidators.validateStartAndCompletionDate(this.editForm.get('startDate'), this.editForm.get('completionDate'))]);
this.editForm.get('completionDate').updateValueAndValidity();
} else {
this.editForm.get('startDate').clearValidators();
}
this.editForm.get('startDate').updateValueAndValidity();
}
)
and
this.editForm.get('completionDate').valueChanges.debounceTime(100).subscribe(
(completionDate: any) => {
if (completionDate != null) {
this.editForm.get('completionDate').setValidators([FormValidators.validateDate, FormValidators.validateStartAndCompletionDate(this.editForm.get('startDate'), this.editForm.get('completionDate'))]);
this.editForm.get('startDate').updateValueAndValidity();
} else {
this.editForm.get('completionDate').clearValidators();
}
this.editForm.get('completionDate').updateValueAndValidity();
}
)
Here is the validator that is setting errors on both the start and end date controls:
static validateStartAndCompletionDate(start: any, completion: any) {
return (f: FormControl) => {
if (start.value != null && completion.value != null) {
var startDate = new Date(start.value.date.year, start.value.date.month - 1, start.value.date.day);
var completionDate = new Date(completion.value.date.year, completion.value.date.month - 1, completion.value.date.day);
if (startDate.getTime() > completionDate.getTime()) {
start.setErrors({
"invalidStartAndCompletion": true
});
completion.setErrors({
"invalidStartAndCompletion": true
});
return { invalidStartAndCompletion: true };
} else {
start.setErrors({
"invalidStartAndCompletion": null
});
completion.setErrors({
"invalidStartAndCompletion": null
});
return null;
}
}
}
}
The validator is working correctly and is setting the errors of each FormControl element accurately. The issue comes when I have to update the validity of each FormControl after the errors are updated. For some reason, with the above code, only the element whose value is being changed is having its validity updated.
For example, if the start date is currently after the completion date (causing a validator error), and I correct the start date to make it before the completion date, the validity of the start date FormControl changes from false to true, but the validity of the completion date FormControl stays false. The opposite is also true where correcting the completion date to fix the error only makes the completion date FormControl valid, not the start date FormControl.
this.editForm.get('startDate').updateValueAndValidity(); this.editForm.get('completionDate').updateValueAndValidity();forstartDatevalidation. My assumption there is thatstartDatevalue is not changed yet, when you try to validatecompletionDate- A. Tim