Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#13968 closed (fixed)

SelectDateWidget does not validate chosen date

Reported by: mitar Owned by: ramiro
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 5 years ago by lemanal

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 5 years ago by lemanal

  • Owner changed from nobody to lemanal
  • Status changed from new to assigned

comment:3 Changed 5 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 5 years ago by lemanal

  • Owner lemanal deleted
  • Status changed from assigned to new

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

comment:5 Changed 5 years ago by mitar

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

comment:6 Changed 5 years ago by ramiro

  • Owner set to ramiro
  • Resolution set to fixed
  • Status changed from new to closed

In [15416]:

(The changeset message doesn't reference this ticket)

comment:7 Changed 5 years ago by ramiro

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