From 2d7049c4ee45e8d4820cdf8106a41a9f4a8331da Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Fri, 4 Feb 2011 23:46:30 +0000 Subject: [PATCH] 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 --- django/forms/extras/widgets.py | 4 ++-- tests/regressiontests/forms/tests/extra.py | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index 8f13ed6c627..0cd88b8170e 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -50,7 +50,7 @@ class SelectDateWidget(Widget): input_format = get_format('DATE_INPUT_FORMATS')[0] # Python 2.4 compatibility: # v = datetime.datetime.strptime(value, input_format) - # would be clearer, but datetime.strptime was added in + # would be clearer, but datetime.strptime was added in # Python 2.5 v = datetime.datetime(*(time.strptime(value, input_format)[0:6])) year_val, month_val, day_val = v.year, v.month, v.day @@ -99,7 +99,7 @@ class SelectDateWidget(Widget): try: date_value = datetime.date(int(y), int(m), int(d)) except ValueError: - pass + return '%s-%s-%s' % (y, m, d) else: date_value = datetime_safe.new_date(date_value) return date_value.strftime(input_format) diff --git a/tests/regressiontests/forms/tests/extra.py b/tests/regressiontests/forms/tests/extra.py index 54f35ee62eb..787a129338a 100644 --- a/tests/regressiontests/forms/tests/extra.py +++ b/tests/regressiontests/forms/tests/extra.py @@ -1,19 +1,18 @@ # -*- coding: utf-8 -*- import datetime -from decimal import Decimal -import re import time from django.conf import settings from django.forms import * from django.forms.extras import SelectDateWidget from django.forms.util import ErrorList -from django.test import TestCase from django.utils import translation from django.utils import unittest from django.utils.encoding import force_unicode from django.utils.encoding import smart_unicode from error_messages import AssertFormErrorsMixin +class GetDate(Form): + mydate = DateField(widget=SelectDateWidget) class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin): ############### @@ -338,9 +337,6 @@ class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin): """) - class GetDate(Form): - mydate = DateField(widget=SelectDateWidget) - a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'}) self.assertTrue(a.is_valid()) self.assertEqual(a.cleaned_data['mydate'], datetime.date(2008, 4, 1)) @@ -355,6 +351,11 @@ class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin): self.assertTrue(b.is_valid()) self.assertEqual(b.cleaned_data['mydate'], datetime.date(2008, 4, 1)) + # Invalid dates shouldn't be allowed + c = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'}) + self.assertFalse(c.is_valid()) + self.assertEqual(c.errors, {'mydate': [u'Enter a valid date.']}) + def test_multiwidget(self): # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually @@ -608,3 +609,10 @@ class FormsExtraL10NTestCase(unittest.TestCase): # Years before 1900 work w = SelectDateWidget(years=('1899',)) self.assertEqual(w.value_from_datadict({'date_year': '1899', 'date_month': '8', 'date_day': '13'}, {}, 'date'), '13-08-1899') + + def test_l10n_invalid_date_in(self): + # Invalid dates shouldn't be allowed + a = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'}) + 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.']})