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
This commit is contained in:
parent
a1cd3c9f52
commit
4a3ad338d6
|
@ -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
|
||||
|
|
|
@ -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 <label>,
|
||||
|
@ -186,12 +193,16 @@ class CheckboxSelectMultiple(SelectMultiple):
|
|||
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_:
|
||||
|
|
|
@ -1471,6 +1471,7 @@ For a form with a <select>, use ChoiceField:
|
|||
<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 @@ MultipleChoiceField is a special case, as its data is required to be a list:
|
|||
<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
|
||||
|
|
Loading…
Reference in New Issue