From 4311402216f92047a4586990f989b39d698e73a8 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Sat, 11 Sep 2010 21:26:19 +0000 Subject: [PATCH] Fixed #13390 -- `SplitDateTimeWidget` now recognizes when it's no longer required. Thanks vaxXxa for bug report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13753 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/fields.py | 2 ++ tests/regressiontests/forms/widgets.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/django/forms/fields.py b/django/forms/fields.py index f6d9c4db54..de14a5c8a8 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -399,6 +399,8 @@ class DateTimeField(Field): # components: date and time. if len(value) != 2: raise ValidationError(self.error_messages['invalid']) + if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES: + return None value = '%s %s' % tuple(value) for format in self.input_formats or formats.get_format('DATETIME_INPUT_FORMATS'): try: diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index b8ec789649..59b33d5210 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -1305,3 +1305,21 @@ class WidgetTests(TestCase): # w2 ought to be independent of w1, since MultiWidget ought # to make a copy of its sub-widgets when it is copied. self.assertEqual(w1.choices, [1,2,3]) + + def test_13390(self): + # See ticket #13390 + class SplitDateForm(forms.Form): + field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=False) + + form = SplitDateForm({'field': ''}) + self.assertTrue(form.is_valid()) + form = SplitDateForm({'field': ['', '']}) + self.assertTrue(form.is_valid()) + + class SplitDateRequiredForm(forms.Form): + field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=True) + + form = SplitDateRequiredForm({'field': ''}) + self.assertFalse(form.is_valid()) + form = SplitDateRequiredForm({'field': ['', '']}) + self.assertFalse(form.is_valid())