Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9124: fixed `SelectDateWidget` with `required=False`. …

…Thanks, Bernd Schlapsi. Backport of [10584] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10585 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 681a26ca9028838a2e790824b7e40b2dff3c252b 1 parent 2ee33cf
Jacob Kaplan-Moss authored April 18, 2009
25  django/forms/extras/widgets.py
@@ -20,13 +20,15 @@ class SelectDateWidget(Widget):
20 20
     This also serves as an example of a Widget that has more than one HTML
21 21
     element and hence implements value_from_datadict.
22 22
     """
  23
+    none_value = (0, '---')
23 24
     month_field = '%s_month'
24 25
     day_field = '%s_day'
25 26
     year_field = '%s_year'
26 27
 
27  
-    def __init__(self, attrs=None, years=None):
  28
+    def __init__(self, attrs=None, years=None, required=True):
28 29
         # years is an optional list/tuple of years to use in the "year" select box.
29 30
         self.attrs = attrs or {}
  31
+        self.required = required
30 32
         if years:
31 33
             self.years = years
32 34
         else:
@@ -51,19 +53,28 @@ def render(self, name, value, attrs=None):
51 53
             id_ = 'id_%s' % name
52 54
 
53 55
         month_choices = MONTHS.items()
  56
+        if not (self.required and value):
  57
+            month_choices.append(self.none_value)
54 58
         month_choices.sort()
55 59
         local_attrs = self.build_attrs(id=self.month_field % id_)
56  
-        select_html = Select(choices=month_choices).render(self.month_field % name, month_val, local_attrs)
  60
+        s = Select(choices=month_choices)
  61
+        select_html = s.render(self.month_field % name, month_val, local_attrs)
57 62
         output.append(select_html)
58 63
 
59 64
         day_choices = [(i, i) for i in range(1, 32)]
  65
+        if not (self.required and value):
  66
+            day_choices.insert(0, self.none_value)
60 67
         local_attrs['id'] = self.day_field % id_
61  
-        select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs)
  68
+        s = Select(choices=day_choices)
  69
+        select_html = s.render(self.day_field % name, day_val, local_attrs)
62 70
         output.append(select_html)
63 71
 
64 72
         year_choices = [(i, i) for i in self.years]
  73
+        if not (self.required and value):
  74
+            year_choices.insert(0, self.none_value)
65 75
         local_attrs['id'] = self.year_field % id_
66  
-        select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs)
  76
+        s = Select(choices=year_choices)
  77
+        select_html = s.render(self.year_field % name, year_val, local_attrs)
67 78
         output.append(select_html)
68 79
 
69 80
         return mark_safe(u'\n'.join(output))
@@ -73,7 +84,11 @@ def id_for_label(self, id_):
73 84
     id_for_label = classmethod(id_for_label)
74 85
 
75 86
     def value_from_datadict(self, data, files, name):
76  
-        y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
  87
+        y = data.get(self.year_field % name)
  88
+        m = data.get(self.month_field % name)
  89
+        d = data.get(self.day_field % name)
  90
+        if y == m == d == "0":
  91
+            return None
77 92
         if y and m and d:
78 93
             return '%s-%s-%s' % (y, m, d)
79 94
         return data.get(name, None)
130  tests/regressiontests/forms/extra.py
@@ -23,6 +23,7 @@
23 23
 >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))
24 24
 >>> print w.render('mydate', '')
25 25
 <select name="mydate_month" id="id_mydate_month">
  26
+<option value="0">---</option>
26 27
 <option value="1">January</option>
27 28
 <option value="2">February</option>
28 29
 <option value="3">March</option>
@@ -37,6 +38,7 @@
37 38
 <option value="12">December</option>
38 39
 </select>
39 40
 <select name="mydate_day" id="id_mydate_day">
  41
+<option value="0">---</option>
40 42
 <option value="1">1</option>
41 43
 <option value="2">2</option>
42 44
 <option value="3">3</option>
@@ -70,6 +72,7 @@
70 72
 <option value="31">31</option>
71 73
 </select>
72 74
 <select name="mydate_year" id="id_mydate_year">
  75
+<option value="0">---</option>
73 76
 <option value="2007">2007</option>
74 77
 <option value="2008">2008</option>
75 78
 <option value="2009">2009</option>
@@ -213,6 +216,133 @@
213 216
 
214 217
 Using a SelectDateWidget in a form:
215 218
 
  219
+>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False)
  220
+>>> print w.render('mydate', '')
  221
+<select name="mydate_month" id="id_mydate_month">
  222
+<option value="0">---</option>
  223
+<option value="1">January</option>
  224
+<option value="2">February</option>
  225
+<option value="3">March</option>
  226
+<option value="4">April</option>
  227
+<option value="5">May</option>
  228
+<option value="6">June</option>
  229
+<option value="7">July</option>
  230
+<option value="8">August</option>
  231
+<option value="9">September</option>
  232
+<option value="10">October</option>
  233
+<option value="11">November</option>
  234
+<option value="12">December</option>
  235
+</select>
  236
+<select name="mydate_day" id="id_mydate_day">
  237
+<option value="0">---</option>
  238
+<option value="1">1</option>
  239
+<option value="2">2</option>
  240
+<option value="3">3</option>
  241
+<option value="4">4</option>
  242
+<option value="5">5</option>
  243
+<option value="6">6</option>
  244
+<option value="7">7</option>
  245
+<option value="8">8</option>
  246
+<option value="9">9</option>
  247
+<option value="10">10</option>
  248
+<option value="11">11</option>
  249
+<option value="12">12</option>
  250
+<option value="13">13</option>
  251
+<option value="14">14</option>
  252
+<option value="15">15</option>
  253
+<option value="16">16</option>
  254
+<option value="17">17</option>
  255
+<option value="18">18</option>
  256
+<option value="19">19</option>
  257
+<option value="20">20</option>
  258
+<option value="21">21</option>
  259
+<option value="22">22</option>
  260
+<option value="23">23</option>
  261
+<option value="24">24</option>
  262
+<option value="25">25</option>
  263
+<option value="26">26</option>
  264
+<option value="27">27</option>
  265
+<option value="28">28</option>
  266
+<option value="29">29</option>
  267
+<option value="30">30</option>
  268
+<option value="31">31</option>
  269
+</select>
  270
+<select name="mydate_year" id="id_mydate_year">
  271
+<option value="0">---</option>
  272
+<option value="2007">2007</option>
  273
+<option value="2008">2008</option>
  274
+<option value="2009">2009</option>
  275
+<option value="2010">2010</option>
  276
+<option value="2011">2011</option>
  277
+<option value="2012">2012</option>
  278
+<option value="2013">2013</option>
  279
+<option value="2014">2014</option>
  280
+<option value="2015">2015</option>
  281
+<option value="2016">2016</option>
  282
+</select>
  283
+>>> print w.render('mydate', '2010-04-15')
  284
+<select name="mydate_month" id="id_mydate_month">
  285
+<option value="0">---</option>
  286
+<option value="1">January</option>
  287
+<option value="2">February</option>
  288
+<option value="3">March</option>
  289
+<option value="4" selected="selected">April</option>
  290
+<option value="5">May</option>
  291
+<option value="6">June</option>
  292
+<option value="7">July</option>
  293
+<option value="8">August</option>
  294
+<option value="9">September</option>
  295
+<option value="10">October</option>
  296
+<option value="11">November</option>
  297
+<option value="12">December</option>
  298
+</select>
  299
+<select name="mydate_day" id="id_mydate_day">
  300
+<option value="0">---</option>
  301
+<option value="1">1</option>
  302
+<option value="2">2</option>
  303
+<option value="3">3</option>
  304
+<option value="4">4</option>
  305
+<option value="5">5</option>
  306
+<option value="6">6</option>
  307
+<option value="7">7</option>
  308
+<option value="8">8</option>
  309
+<option value="9">9</option>
  310
+<option value="10">10</option>
  311
+<option value="11">11</option>
  312
+<option value="12">12</option>
  313
+<option value="13">13</option>
  314
+<option value="14">14</option>
  315
+<option value="15" selected="selected">15</option>
  316
+<option value="16">16</option>
  317
+<option value="17">17</option>
  318
+<option value="18">18</option>
  319
+<option value="19">19</option>
  320
+<option value="20">20</option>
  321
+<option value="21">21</option>
  322
+<option value="22">22</option>
  323
+<option value="23">23</option>
  324
+<option value="24">24</option>
  325
+<option value="25">25</option>
  326
+<option value="26">26</option>
  327
+<option value="27">27</option>
  328
+<option value="28">28</option>
  329
+<option value="29">29</option>
  330
+<option value="30">30</option>
  331
+<option value="31">31</option>
  332
+</select>
  333
+<select name="mydate_year" id="id_mydate_year">
  334
+<option value="0">---</option>
  335
+<option value="2007">2007</option>
  336
+<option value="2008">2008</option>
  337
+<option value="2009">2009</option>
  338
+<option value="2010" selected="selected">2010</option>
  339
+<option value="2011">2011</option>
  340
+<option value="2012">2012</option>
  341
+<option value="2013">2013</option>
  342
+<option value="2014">2014</option>
  343
+<option value="2015">2015</option>
  344
+<option value="2016">2016</option>
  345
+</select>
216 346
 >>> class GetDate(Form):
217 347
 ...     mydate = DateField(widget=SelectDateWidget)
218 348
 >>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})

0 notes on commit 681a26c

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