From 36be3febef28ac45404b59dd59aa6d9f0185acae Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 12 Aug 2007 02:15:35 +0000 Subject: [PATCH] Fixed #4622 -- Fixed SelectDateWidget to work correctly when used as a hidden input field. Thanks, Bill Fenner. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5859 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/extras/widgets.py | 2 +- tests/regressiontests/forms/tests.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/django/newforms/extras/widgets.py b/django/newforms/extras/widgets.py index 96b1c7244d2..60936a6bd68 100644 --- a/django/newforms/extras/widgets.py +++ b/django/newforms/extras/widgets.py @@ -57,4 +57,4 @@ class SelectDateWidget(Widget): y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name) if y and m and d: return '%s-%s-%s' % (y, m, d) - return None + return data.get(name, None) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 2386a7f8b17..78442677eb0 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -3635,6 +3635,29 @@ True +Using a SelectDateWidget in a form: + +>>> class GetDate(Form): +... mydate = DateField(widget=SelectDateWidget) +>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'}) +>>> print a.is_valid() +True +>>> print a.cleaned_data['mydate'] +2008-04-01 + +As with any widget that implements get_value_from_datadict, +we must be prepared to accept the input from the "as_hidden" +rendering as well. + +>>> print a['mydate'].as_hidden() + +>>> b=GetDate({'mydate':'2008-4-1'}) +>>> print b.is_valid() +True +>>> print b.cleaned_data['mydate'] +2008-04-01 + + # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually # combined with MultiValueFields - a field that is composed of other fields.