Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15416 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2d7049c4ee45e8d4820cdf8106a41a9f4a8331da 1 parent 0ffa39f
Ramiro Morales authored February 04, 2011
4  django/forms/extras/widgets.py
@@ -50,7 +50,7 @@ def render(self, name, value, attrs=None):
50 50
                         input_format = get_format('DATE_INPUT_FORMATS')[0]
51 51
                         # Python 2.4 compatibility:
52 52
                         #     v = datetime.datetime.strptime(value, input_format)
53  
-                        # would be clearer, but datetime.strptime was added in 
  53
+                        # would be clearer, but datetime.strptime was added in
54 54
                         # Python 2.5
55 55
                         v = datetime.datetime(*(time.strptime(value, input_format)[0:6]))
56 56
                         year_val, month_val, day_val = v.year, v.month, v.day
@@ -99,7 +99,7 @@ def value_from_datadict(self, data, files, name):
99 99
                 try:
100 100
                     date_value = datetime.date(int(y), int(m), int(d))
101 101
                 except ValueError:
102  
-                    pass
  102
+                    return '%s-%s-%s' % (y, m, d)
103 103
                 else:
104 104
                     date_value = datetime_safe.new_date(date_value)
105 105
                     return date_value.strftime(input_format)
20  tests/regressiontests/forms/tests/extra.py
... ...
@@ -1,19 +1,18 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import datetime
3  
-from decimal import Decimal
4  
-import re
5 3
 import time
6 4
 from django.conf import settings
7 5
 from django.forms import *
8 6
 from django.forms.extras import SelectDateWidget
9 7
 from django.forms.util import ErrorList
10  
-from django.test import TestCase
11 8
 from django.utils import translation
12 9
 from django.utils import unittest
13 10
 from django.utils.encoding import force_unicode
14 11
 from django.utils.encoding import smart_unicode
15 12
 from error_messages import AssertFormErrorsMixin
16 13
 
  14
+class GetDate(Form):
  15
+    mydate = DateField(widget=SelectDateWidget)
17 16
 
18 17
 class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin):
19 18
     ###############
@@ -338,9 +337,6 @@ def test_selectdate(self):
338 337
 <option value="2016">2016</option>
339 338
 </select>""")
340 339
 
341  
-        class GetDate(Form):
342  
-            mydate = DateField(widget=SelectDateWidget)
343  
-
344 340
         a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
345 341
         self.assertTrue(a.is_valid())
346 342
         self.assertEqual(a.cleaned_data['mydate'], datetime.date(2008, 4, 1))
@@ -355,6 +351,11 @@ class GetDate(Form):
355 351
         self.assertTrue(b.is_valid())
356 352
         self.assertEqual(b.cleaned_data['mydate'], datetime.date(2008, 4, 1))
357 353
 
  354
+        # Invalid dates shouldn't be allowed
  355
+        c = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'})
  356
+        self.assertFalse(c.is_valid())
  357
+        self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']})
  358
+
358 359
     def test_multiwidget(self):
359 360
         # MultiWidget and MultiValueField #############################################
360 361
         # MultiWidgets are widgets composed of other widgets. They are usually
@@ -608,3 +609,10 @@ def test_l10n(self):
608 609
         # Years before 1900 work
609 610
         w = SelectDateWidget(years=('1899',))
610 611
         self.assertEqual(w.value_from_datadict({'date_year': '1899', 'date_month': '8', 'date_day': '13'}, {}, 'date'), '13-08-1899')
  612
+
  613
+    def test_l10n_invalid_date_in(self):
  614
+        # Invalid dates shouldn't be allowed
  615
+        a = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'})
  616
+        self.assertFalse(a.is_valid())
  617
+        # 'Geef een geldige datum op.' = 'Enter a valid date.'
  618
+        self.assertEqual(a.errors, {'mydate': [u'Geef een geldige datum op.']})

0 notes on commit 2d7049c

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