DatePicker component behaviour

As expected, I cannot enter an out of range date using my mouse & the DatePicker GUI, if I’ve set min-date and max_date properties. However, I can enter an out of range date manually in the DatePicker text property field of the DatePicker GUI. It isn’t the same property as the date property, of course, but it looks like it is, to the user…

For instance, if I’ve got, “to” and “from” DatePickers and a “Search” button and I set the dates with my mouse via the GUI, but then alter one of the dates (text representation of the dates) manually, to an out of range value, which I can actually do, also, via the GUI text, before I press the “Search” button, it won’t be the search that I thought I was specifying when I do actually press the “Search” button. My text edit is ignored and one of the date parameters of the search will be one of min_date, or max_date…

Is there a way to prevent this potentially confusing behaviour?

Does the date-picker’s On Change event (https://anvil.works/docs/api/anvil#DatePicker_events) help you here?

my event handler skeleton just changes the date to an int at the moment

def from_date_picker_change(self, **event_args):
   """This method is called when the selected date changes"""
       self.from_date=self.date2int(self.from_date_picker.date)

but the problem remains - I shouldn’t be able to fool myself that I have actually altered the DatePicker date by fiddling around with the DatePicker text.

The problem doesn’t arise if you don’t set min_date and max_date. The text and the date in the DatePicker just synchronise. If you do set min_date and max_date the synchronisation only occurs as far as min_date & max_date and then things get wierd

here’s a vid that demos what I’m on about in case it isn’t obvious…

(Attachment DatePicker.mp4 is missing)

Darn it! I cannot share my vid. Never mind, here are two pics

Docs say that you can look at the text of the date widget, and I’d hoped that would give you a way to check for this case. But I tried it, and text is not available after all. For now, perhaps the best that can be done is to check (in the change event handler) whether the limit has been hit, and if so,

  1. pop up a warning
  2. force the focus back to the date widget

This might require setting the limits slightly wider…

Cheers. As I get more data the setting limits will widen. I will play with your idea & see how it works!

I agree, having the displayed text disagree with the selected date is a problem, if not a downright bug. I can’t file this under Bug Reports, but I hope someone (in an official capacity) will do so.

1 Like

This definitely looks like a bug - maybe 2 bugs:

  1. datepicker.text property has been removed but not from the documentation.
  2. datepicker text does not reflect the date if changed by typing and outside of the date range

The best approach I can see is to do the following:

def from_date_picker_change(self, **event_args):
   """This method is called when the selected date changes"""
   self.from_date_picker.date = self.from_date_picker.date
   self.from_date=self.date2int(self.from_date_picker.date)

This will set the text to the date and will be the user experience that you intend. If they try to type a date outside of the range it will be reset to the min/max date for that datepicker.


side note:
depending on how you want to get the date2int you could use self.from_date_picker.date.toordinal()

1 Like

Hi folks,

Thanks for the heads-up here, and sorry for the confusion. As you say, there is at least one bug here (the incorrect documentation of the text property). We’ll get that fixed, and I also recommend the approach suggested by @stucork for catching dates that are out of range for now - we may well make that the built-in behaviour.

Moved to bug reports.

2 Likes