Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#13968 closed (fixed)

SelectDateWidget does not validate chosen date

Reported by: Mitar Owned by: Ramiro Morales
Component: Forms Version: 1.2
Severity: Keywords:
Cc: mmitar@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

SelectDateWidget does not validate chosen date and does not show an error to the user about invalid date (like 31 February) but silently discards it and resets/nullify the value. So user does not get any feedback and possibility to correct the date.

This is with USE_L10N set to True.

The problem is probably that any ValueError errors from datetime are silently discarded and input date is taken as it is invalid format not invalid date.

Change History (7)

comment:1 Changed 6 years ago by Alex Lemann

Triage Stage: UnreviewedAccepted

Here is code to reproduce the bug:

In [1]: from django import forms

In [2]: from django.forms.extras.widgets import SelectDateWidget

In [3]: class DateForm(forms.Form):
   ...:     date = forms.DateField(widget=SelectDateWidget)
   ...: 

In [4]: form = DateForm({'date_month': '2', 'date_day': '31', 'date_year': '2010' })

In [5]: form.is_valid()
Out[5]: False

In [6]: form.errors
Out[6]: {'date': [u'This field is required.']}

At the very least, the error here should be "invalid date".

comment:2 Changed 6 years ago by Alex Lemann

Owner: changed from nobody to Alex Lemann
Status: newassigned

comment:3 Changed 6 years ago by gptvnt

Looks like it's fixed in the latest trunk. Following the same example, I get the following -

>>> from django import forms
>>> from django.forms.extras.widgets import SelectDateWidget
>>> class DateForm(forms.Form):
...     date = forms.DateField(widget=SelectDateWidget)
... 
>>> form = DateForm({'date_month':'2', 'date_day':'31', 'date_year':'2010'})
>>> form.is_valid()
False
>>> form.errors
{'date': [u'Enter a valid date.']}

comment:4 Changed 6 years ago by Alex Lemann

Owner: Alex Lemann deleted
Status: assignednew

This still shows up for me when I explicitly set USE_L10N=True in settings.py but not otherwise.

comment:5 Changed 6 years ago by Mitar

As I reported, the problem is only with USE_L10N set to True (default is False).

comment:6 Changed 6 years ago by Ramiro Morales

Owner: set to Ramiro Morales
Resolution: fixed
Status: newclosed

In [15416]:

(The changeset message doesn't reference this ticket)

comment:7 Changed 6 years ago by Ramiro Morales

In [15417]:

[1.2.X] Fixed #13968 -- Fixed SelectDateWidget processing of an invalid date input value when USE_L10N is on, for consistency with its behavior when USE_L10N=False (now the form field reports the validation error in both cases). Thanks mitar for the report.

Backport of [15416] from trunk

Note: See TracTickets for help on using tickets.
Back to Top