sh977218 / ts-etl-ui

0 stars 0 forks source link

Validate that Schedule date is in the future #71

Closed ludetc closed 4 months ago

ludetc commented 4 months ago
export function futureDateValidator(): ValidatorFn {
  return (control: AbstractControl): { [key: string]: any } | null => {
    const date = new Date(control.value);
    const today = new Date();

    // Reset the time to compare only dates
    today.setHours(0, 0, 0, 0);

    return date > today ? null : { notFutureDate: { value: control.value } };
  };
}
sh977218 commented 4 months ago

we can also add a date picker filter, so that pasted dates are disabled from selection: https://material.angular.io/components/datepicker/examples Datepicker with filter validation

sh977218 commented 4 months ago
export function futureDateValidator(): ValidatorFn {
  return (control: AbstractControl): { [key: string]: any } | null => {
    const date = new Date(control.value);
    const today = new Date();

    // Reset the time to compare only dates
    today.setHours(0, 0, 0, 0);

    return date > today ? null : { notFutureDate: { value: control.value } };
  };
}

A benefit of using validator is that it propagate up all the way to the form level validation, so the submit/save button can be disabled on all those validators. This approach does not need to introduce any new variables in the ts file.

ludetc commented 4 months ago

I think I like your datepicker option best

sh977218 commented 4 months ago

I think I like your datepicker option best

I'm not saying pick one, I think we should do both. They function differently. one is for form validator, another is for better user experience. User can manipulate the input any way they want, the date picker will not prevent they change the input value.