I have 2 input in reactive forms. for example: birthdate and date, I want to add Validation to date, that date's minimal value must be birthdate or more. How can I solve it?
I try this way, but it does not work.
Validators.min(this.myForm.value.birthDate);
It would be even more helpful if you have attached Typescript file but I am attaching the code of custom validator here. Please adjust name of forms, fields according to your code. In short for these kinfd of validations you have to write custom validations. In built validations won't help here.
export class CustomeDateValidators {
static fromToDate(fromDateField: string, toDateField: string, errorName: string = 'fromToDate'): ValidatorFn {
return (formGroup: AbstractControl): { [key: string]: boolean } | null => {
const fromDate = formGroup.get(fromDateField).value;
const toDate = formGroup.get(toDateField).value;
// Ausing the fromDate and toDate are numbers. In not convert them first after null check
if ((fromDate !== null && toDate !== null) && fromDate > toDate) {
return {[errorName]: true};
}
return null;
};
}
}
/*--- implementations ---*/
this.form = this.fb.group({
fromDate: null,
toDate: null,
}, { validator: [
//Default error with this validator: {fromToDate: true}
CustomeDateValidators.fromToDate('fromDate', 'toDate')
// For custome error name like: {customeErrorName: true}, pass third optional parameter with custome name
// CustomeDateValidators.fromToDate('fromDate', 'toDate', 'customeErrorName')
]});