Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit f2b0f8ea0fc5849801a2aeb71f2db106e5655aea 1 parent 8ce7bea
Ramiro Morales authored February 05, 2011
44  django/forms/extras/widgets.py
@@ -17,6 +17,26 @@
17 17
 
18 18
 RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
19 19
 
  20
+def _parse_date_fmt():
  21
+    fmt = get_format('DATE_FORMAT')
  22
+    escaped = False
  23
+    output = []
  24
+    for char in fmt:
  25
+        if escaped:
  26
+            escaped = False
  27
+        elif char == '\\':
  28
+            escaped = True
  29
+        elif char in 'Yy':
  30
+            output.append('year')
  31
+            #if not self.first_select: self.first_select = 'year'
  32
+        elif char in 'bEFMmNn':
  33
+            output.append('month')
  34
+            #if not self.first_select: self.first_select = 'month'
  35
+        elif char in 'dj':
  36
+            output.append('day')
  37
+            #if not self.first_select: self.first_select = 'day'
  38
+    return output
  39
+
20 40
 class SelectDateWidget(Widget):
21 41
     """
22 42
     A Widget that splits date input into three <select> boxes.
@@ -67,24 +87,25 @@ def render(self, name, value, attrs=None):
67 87
         choices = [(i, i) for i in range(1, 32)]
68 88
         day_html = self.create_select(name, self.day_field, value, day_val,  choices)
69 89
 
70  
-        format = get_format('DATE_FORMAT')
71  
-        escaped = False
72 90
         output = []
73  
-        for char in format:
74  
-            if escaped:
75  
-                escaped = False
76  
-            elif char == '\\':
77  
-                escaped = True
78  
-            elif char in 'Yy':
  91
+        for field in _parse_date_fmt():
  92
+            if field == 'year':
79 93
                 output.append(year_html)
80  
-            elif char in 'bEFMmNn':
  94
+            elif field == 'month':
81 95
                 output.append(month_html)
82  
-            elif char in 'dj':
  96
+            elif field == 'day':
83 97
                 output.append(day_html)
84 98
         return mark_safe(u'\n'.join(output))
85 99
 
86 100
     def id_for_label(self, id_):
87  
-        return '%s_month' % id_
  101
+        first_select = None
  102
+        field_list = _parse_date_fmt()
  103
+        if field_list:
  104
+            first_select = field_list[0]
  105
+        if first_select is not None:
  106
+            return '%s_%s' % (id_, first_select)
  107
+        else:
  108
+            return '%s_month' % id_
88 109
     id_for_label = classmethod(id_for_label)
89 110
 
90 111
     def value_from_datadict(self, data, files, name):
@@ -118,4 +139,3 @@ def create_select(self, name, field, value, val, choices):
118 139
         s = Select(choices=choices)
119 140
         select_html = s.render(field % name, val, local_attrs)
120 141
         return select_html
121  
-
9  tests/regressiontests/forms/tests/extra.py
@@ -356,6 +356,10 @@ def test_selectdate(self):
356 356
         self.assertFalse(c.is_valid())
357 357
         self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']})
358 358
 
  359
+        # label tag is correctly associated with month dropdown
  360
+        d = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
  361
+        self.assertTrue('<label for="id_mydate_month">' in d.as_p())
  362
+
359 363
     def test_multiwidget(self):
360 364
         # MultiWidget and MultiValueField #############################################
361 365
         # MultiWidgets are widgets composed of other widgets. They are usually
@@ -616,3 +620,8 @@ def test_l10n_invalid_date_in(self):
616 620
         self.assertFalse(a.is_valid())
617 621
         # 'Geef een geldige datum op.' = 'Enter a valid date.'
618 622
         self.assertEqual(a.errors, {'mydate': [u'Geef een geldige datum op.']})
  623
+
  624
+    def test_form_label_association(self):
  625
+        # label tag is correctly associated with first rendered dropdown
  626
+        a = GetDate({'mydate_month':'1', 'mydate_day':'1', 'mydate_year':'2010'})
  627
+        self.assertTrue('<label for="id_mydate_day">' in a.as_p())

0 notes on commit f2b0f8e

Please sign in to comment.
Something went wrong with that request. Please try again.