From 4a3ad338d67c2412fb4c0f7b44b7a5651a7ec1a6 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 29 Nov 2006 17:00:34 +0000 Subject: [PATCH] newforms: Added Widget.value_from_datadict hook, which allows a Widget to define how to convert its post data dictionary to a value. Implemented it for CheckboxSelectMultiple and updated unit tests git-svn-id: http://code.djangoproject.com/svn/django/trunk@4136 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 5 +++- django/newforms/widgets.py | 13 ++++++++- tests/regressiontests/forms/tests.py | 41 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index b8911dd013..8b58b94ccd 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -118,7 +118,10 @@ class Form(object): self.__errors = errors return for name, field in self.fields.items(): - value = self.data.get(name, None) + # value_from_datadict() gets the data from the dictionary. + # Each widget type knows how to retrieve its own data, because some + # widgets split data over several HTML fields. + value = field.widget.value_from_datadict(self.data, name) try: value = field.clean(value) self.clean_data[name] = value diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 1e7c839969..51bbaf0896 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -36,6 +36,13 @@ class Widget(object): attrs.update(extra_attrs) return attrs + def value_from_datadict(self, data, name): + """ + Given a dictionary of data and this widget's name, returns the value + of this widget. Returns None if it's not provided. + """ + return data.get(name, None) + def id_for_label(self, id_): """ Returns the HTML ID attribute of this Widget for use by a