Fixed #7195 -- Fixed the validation of MultipleChoice fields so that they can

be populated from request.REQUEST. Based on a patch from Daniel Roseman.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8525 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-08-25 00:32:32 +00:00
parent d6e5632969
commit 6d6fb392b4
2 changed files with 32 additions and 26 deletions

View File

@ -10,7 +10,7 @@ except NameError:
import copy import copy
from itertools import chain from itertools import chain
from django.conf import settings from django.conf import settings
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict, MergeDict
from django.utils.html import escape, conditional_escape from django.utils.html import escape, conditional_escape
from django.utils.translation import ugettext from django.utils.translation import ugettext
from django.utils.encoding import StrAndUnicode, force_unicode from django.utils.encoding import StrAndUnicode, force_unicode
@ -250,7 +250,7 @@ class MultipleHiddenInput(HiddenInput):
for v in value])) for v in value]))
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
if isinstance(data, MultiValueDict): if isinstance(data, (MultiValueDict, MergeDict)):
return data.getlist(name) return data.getlist(name)
return data.get(name, None) return data.get(name, None)
@ -417,7 +417,7 @@ class SelectMultiple(Select):
return mark_safe(u'\n'.join(output)) return mark_safe(u'\n'.join(output))
def value_from_datadict(self, data, files, name): def value_from_datadict(self, data, files, name):
if isinstance(data, MultiValueDict): if isinstance(data, (MultiValueDict, MergeDict)):
return data.getlist(name) return data.getlist(name)
return data.get(name, None) return data.get(name, None)

View File

@ -540,8 +540,9 @@ zero-based index.
<li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li> <li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
</ul> </ul>
Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict Data for a MultipleChoiceField should be a list. QueryDict, MultiValueDict and
conveniently work with this. MergeDict (when created as a merge of MultiValueDicts) conveniently work with
this.
>>> data = {'name': 'Yesterday', 'composers': ['J', 'P']} >>> data = {'name': 'Yesterday', 'composers': ['J', 'P']}
>>> f = SongForm(data) >>> f = SongForm(data)
>>> f.errors >>> f.errors
@ -556,6 +557,11 @@ conveniently work with this.
>>> f = SongForm(data) >>> f = SongForm(data)
>>> f.errors >>> f.errors
{} {}
>>> from django.utils.datastructures import MergeDict
>>> data = MergeDict(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])))
>>> f = SongForm(data)
>>> f.errors
{}
The MultipleHiddenInput widget renders multiple values as hidden fields. The MultipleHiddenInput widget renders multiple values as hidden fields.
>>> class SongFormHidden(Form): >>> class SongFormHidden(Form):