Skip to content

Commit

Permalink
Fixed #15226 - Made SelectDateWidget render the label tag associated …
Browse files Browse the repository at this point in the history
…with the correct dropdown sub-widget when USE_L10N is active and non-English locale is in use.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15427 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Feb 5, 2011
1 parent 8ce7bea commit f2b0f8e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
44 changes: 32 additions & 12 deletions django/forms/extras/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@

RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')

def _parse_date_fmt():
fmt = get_format('DATE_FORMAT')
escaped = False
output = []
for char in fmt:
if escaped:
escaped = False
elif char == '\\':
escaped = True
elif char in 'Yy':
output.append('year')
#if not self.first_select: self.first_select = 'year'
elif char in 'bEFMmNn':
output.append('month')
#if not self.first_select: self.first_select = 'month'
elif char in 'dj':
output.append('day')
#if not self.first_select: self.first_select = 'day'
return output

class SelectDateWidget(Widget):
"""
A Widget that splits date input into three <select> boxes.
Expand Down Expand Up @@ -67,24 +87,25 @@ def render(self, name, value, attrs=None):
choices = [(i, i) for i in range(1, 32)]
day_html = self.create_select(name, self.day_field, value, day_val, choices)

format = get_format('DATE_FORMAT')
escaped = False
output = []
for char in format:
if escaped:
escaped = False
elif char == '\\':
escaped = True
elif char in 'Yy':
for field in _parse_date_fmt():
if field == 'year':
output.append(year_html)
elif char in 'bEFMmNn':
elif field == 'month':
output.append(month_html)
elif char in 'dj':
elif field == 'day':
output.append(day_html)
return mark_safe(u'\n'.join(output))

def id_for_label(self, id_):
return '%s_month' % id_
first_select = None
field_list = _parse_date_fmt()
if field_list:
first_select = field_list[0]
if first_select is not None:
return '%s_%s' % (id_, first_select)
else:
return '%s_month' % id_
id_for_label = classmethod(id_for_label)

def value_from_datadict(self, data, files, name):
Expand Down Expand Up @@ -118,4 +139,3 @@ def create_select(self, name, field, value, val, choices):
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html

9 changes: 9 additions & 0 deletions tests/regressiontests/forms/tests/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ def test_selectdate(self):
self.assertFalse(c.is_valid())
self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']})

# label tag is correctly associated with month dropdown
d = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
self.assertTrue('<label for="id_mydate_month">' in d.as_p())

def test_multiwidget(self):
# MultiWidget and MultiValueField #############################################
# MultiWidgets are widgets composed of other widgets. They are usually
Expand Down Expand Up @@ -616,3 +620,8 @@ def test_l10n_invalid_date_in(self):
self.assertFalse(a.is_valid())
# 'Geef een geldige datum op.' = 'Enter a valid date.'
self.assertEqual(a.errors, {'mydate': [u'Geef een geldige datum op.']})

def test_form_label_association(self):
# label tag is correctly associated with first rendered dropdown
a = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
self.assertTrue('<label for="id_mydate_day">' in a.as_p())

0 comments on commit f2b0f8e

Please sign in to comment.