javagwtsmartgwt

Set range limits on DateTimeItem


What I need specifically is to prevent user from entering date/time that is in the future. That's different than disabling a specific date, since I need to disable ALL dates part a certain date.

Ideally, any future dates should be disabled. For now, I'm just going prevent form submit when user enters invalid time, but disabling unwanted dates would be better.

I went through the javadoc and found nothing. Is it possible? How?


Solution

  • Configuring a range just means disabling the dates outside this range. So the process is the same as the one in the linked answer. You can create a utility method to create filters easier. For example, configureShowRangeHandler admits a Predicate<Date> that will disable the date if the predicate returns false. The enableUntilToday is a simple example to limit selectable dates until today.

    {
        DatePicker dp = new DatePicker();
        Predicate<Date> enableUntilTodayDates = d -> !d.after(new Date());
        configureShowRangeHandler(dp, enableUntilTodayDates);
    }
    
    static HandlerRegistration configureShowRangeHandler(DatePicker dp, Predicate<Date> fn) {
        return dp.addShowRangeHandler(ev -> {
            for (Date t = copyDate(ev.getStart()); t.before(ev.getEnd()); addDaysToDate(t, 1)) {
                dp.setTransientEnabledOnDates(fn.test(t), t);
            }
        });
    }