I'm trying to implement a custom angular validator for date range checking.
The validator itself works properly and returns a validation error. However, nothing seems to be happening at the client side - no errors are being shown and the form is considered to be valid. I've tried various changes to this code, with no joy.
Any ideas on what to try?
Html:
<div class="alert-danger" *ngIf="form.controls.creditCheckDate.errors?.dateRange">
Date should be from 1/1/2000 to Today.
</div>
.ts:
const controlsConfig = {
creditCheckDate: ['', [Validators.required,
CustomValidators.dateRange(new Date(2000, 1), new Date(Date.now()))]]
};
return this.fb.group(controlsConfig);
Validator:
static dateRange(minDate: Date | null, maxDate: Date | null): ValidatorFn {
return (c: AbstractControl): ValidationErrors | null => {
const validationError = { dateRange: true };
if (!c.value) {
return null;
}
const timestamp = Date.parse(c.value);
if (isNaN(timestamp)) {
return validationError;
}
const date = new Date(timestamp);
if ((minDate && minDate > date) || (maxDate && maxDate < date)) {
return validationError;
}
return null;
};
}
*ngIf="NameOfYourForm?.hasError('dateRange')"