Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

newforms: Added Widget.value_from_datadict hook, which allows a Widge…

…t 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
  • Loading branch information...
commit 4a3ad338d67c2412fb4c0f7b44b7a5651a7ec1a6 1 parent a1cd3c9
@adrianholovaty adrianholovaty authored
View
5 django/newforms/forms.py
@@ -118,7 +118,10 @@ def full_clean(self):
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
View
13 django/newforms/widgets.py
@@ -36,6 +36,13 @@ def build_attrs(self, extra_attrs=None, **kwargs):
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 <label>,
@@ -186,12 +193,16 @@ def render(self, name, value, attrs=None, choices=()):
cb = CheckboxInput(final_attrs)
for option_value, option_label in chain(self.choices, choices):
option_value = smart_unicode(option_value)
- field_name = unicode(name + option_value)
+ field_name = name + option_value
rendered_cb = cb.render(field_name, (option_value in str_values))
output.append(u'<li><label>%s %s</label></li>' % (rendered_cb, escape(smart_unicode(option_label))))
output.append(u'</ul>')
return u'\n'.join(output)
+ def value_from_datadict(self, data, name):
+ data_list = [k for k, v in self.choices if data.get(name + k)]
+ return data_list or None
+
def id_for_label(self, id_):
# See the comment for RadioSelect.id_for_label()
if id_:
View
41 tests/regressiontests/forms/tests.py
@@ -1471,6 +1471,7 @@
<option value="J">Java</option>
</select>
+Add widget=RadioSelect to use that widget with a ChoiceField.
>>> class FrameworkForm(Form):
... name = CharField()
... language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect)
@@ -1545,6 +1546,46 @@
<option value="P" selected="selected">Paul McCartney</option>
</select>
+MultipleChoiceField can also be used with the CheckboxSelectMultiple widget.
+>>> class SongForm(Form):
+... name = CharField()
+... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=CheckboxSelectMultiple)
+>>> f = SongForm()
+>>> print f['composers']
+<ul>
+<li><label><input type="checkbox" name="composersJ" /> John Lennon</label></li>
+<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li>
+</ul>
+>>> f = SongForm({'composers': ['J']})
+>>> print f['composers']
+<ul>
+<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li>
+<li><label><input type="checkbox" name="composersP" /> Paul McCartney</label></li>
+</ul>
+>>> f = SongForm({'composers': ['J', 'P']})
+>>> print f['composers']
+<ul>
+<li><label><input checked="checked" type="checkbox" name="composersJ" /> John Lennon</label></li>
+<li><label><input checked="checked" type="checkbox" name="composersP" /> Paul McCartney</label></li>
+</ul>
+
+When using CheckboxSelectMultiple, the framework automatically converts the
+data in clean_data to a list of values, rather than the underlying HTML form
+field name.
+>>> f = SongForm({'name': 'Yesterday'})
+>>> f.errors
+{'composers': [u'This field is required.']}
+>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on'})
+>>> f.errors
+{}
+>>> f.clean_data
+{'composers': [u'J'], 'name': u'Yesterday'}
+>>> f = SongForm({'name': 'Yesterday', 'composersJ': 'on', 'composersP': 'on'})
+>>> f.errors
+{}
+>>> f.clean_data
+{'composers': [u'J', u'P'], 'name': u'Yesterday'}
+
There are a couple of ways to do multiple-field validation. If you want the
validation message to be associated with a particular field, implement the
clean_XXX() method on the Form, where XXX is the field name. As in

0 comments on commit 4a3ad33

Please sign in to comment.
Something went wrong with that request. Please try again.