Fixed #15226 - Made SelectDateWidget render the label tag associated 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
This commit is contained in:
Ramiro Morales 2011-02-05 18:16:27 +00:00
parent 8ce7beaea0
commit f2b0f8ea0f
2 changed files with 41 additions and 12 deletions

View File

@ -17,6 +17,26 @@ __all__ = ('SelectDateWidget',)
RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$') 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): class SelectDateWidget(Widget):
""" """
A Widget that splits date input into three <select> boxes. A Widget that splits date input into three <select> boxes.
@ -67,23 +87,24 @@ class SelectDateWidget(Widget):
choices = [(i, i) for i in range(1, 32)] choices = [(i, i) for i in range(1, 32)]
day_html = self.create_select(name, self.day_field, value, day_val, choices) day_html = self.create_select(name, self.day_field, value, day_val, choices)
format = get_format('DATE_FORMAT')
escaped = False
output = [] output = []
for char in format: for field in _parse_date_fmt():
if escaped: if field == 'year':
escaped = False
elif char == '\\':
escaped = True
elif char in 'Yy':
output.append(year_html) output.append(year_html)
elif char in 'bEFMmNn': elif field == 'month':
output.append(month_html) output.append(month_html)
elif char in 'dj': elif field == 'day':
output.append(day_html) output.append(day_html)
return mark_safe(u'\n'.join(output)) return mark_safe(u'\n'.join(output))
def id_for_label(self, id_): def id_for_label(self, 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_ return '%s_month' % id_
id_for_label = classmethod(id_for_label) id_for_label = classmethod(id_for_label)
@ -118,4 +139,3 @@ class SelectDateWidget(Widget):
s = Select(choices=choices) s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs) select_html = s.render(field % name, val, local_attrs)
return select_html return select_html

View File

@ -356,6 +356,10 @@ class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin):
self.assertFalse(c.is_valid()) self.assertFalse(c.is_valid())
self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']}) 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): def test_multiwidget(self):
# MultiWidget and MultiValueField ############################################# # MultiWidget and MultiValueField #############################################
# MultiWidgets are widgets composed of other widgets. They are usually # MultiWidgets are widgets composed of other widgets. They are usually
@ -616,3 +620,8 @@ class FormsExtraL10NTestCase(unittest.TestCase):
self.assertFalse(a.is_valid()) self.assertFalse(a.is_valid())
# 'Geef een geldige datum op.' = 'Enter a valid date.' # 'Geef een geldige datum op.' = 'Enter a valid date.'
self.assertEqual(a.errors, {'mydate': [u'Geef een geldige datum op.']}) 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())