Skip to content

Commit 4311402

Browse files
committed
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
1 parent 9e04c3b commit 4311402

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

django/forms/fields.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ def to_python(self, value):
399399
# components: date and time.
400400
if len(value) != 2:
401401
raise ValidationError(self.error_messages['invalid'])
402+
if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES:
403+
return None
402404
value = '%s %s' % tuple(value)
403405
for format in self.input_formats or formats.get_format('DATETIME_INPUT_FORMATS'):
404406
try:

tests/regressiontests/forms/widgets.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,3 +1305,21 @@ def test_12048(self):
13051305
# w2 ought to be independent of w1, since MultiWidget ought
13061306
# to make a copy of its sub-widgets when it is copied.
13071307
self.assertEqual(w1.choices, [1,2,3])
1308+
1309+
def test_13390(self):
1310+
# See ticket #13390
1311+
class SplitDateForm(forms.Form):
1312+
field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=False)
1313+
1314+
form = SplitDateForm({'field': ''})
1315+
self.assertTrue(form.is_valid())
1316+
form = SplitDateForm({'field': ['', '']})
1317+
self.assertTrue(form.is_valid())
1318+
1319+
class SplitDateRequiredForm(forms.Form):
1320+
field = forms.DateTimeField(widget=forms.SplitDateTimeWidget, required=True)
1321+
1322+
form = SplitDateRequiredForm({'field': ''})
1323+
self.assertFalse(form.is_valid())
1324+
form = SplitDateRequiredForm({'field': ['', '']})
1325+
self.assertFalse(form.is_valid())

0 commit comments

Comments
 (0)