diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 4e4fc0c80b..4bbd02dc63 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -92,11 +92,21 @@ class MultipleHiddenInput(HiddenInput): A widget that handles for fields that have a list of values. """ - def render(self, name, value, attrs=None): + def __init__(self, attrs=None, choices=()): + # choices can be any iterable + self.attrs = attrs or {} + self.choices = choices + + def render(self, name, value, attrs=None, choices=()): if value is None: value = [] final_attrs = self.build_attrs(attrs, type=self.input_type, name=name) return u'\n'.join([(u'' % flatatt(dict(value=smart_unicode(v), **final_attrs))) for v in value]) + def value_from_datadict(self, data, name): + if isinstance(data, MultiValueDict): + return data.getlist(name) + return data.get(name, None) + class FileInput(Input): input_type = 'file' diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 16d71edd55..7ad6ffb98a 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1950,11 +1950,20 @@ conveniently work with this. >>> f.errors {} >>> from django.utils.datastructures import MultiValueDict ->>> data = MultiValueDict(dict(name='Yesterday', composers=['J', 'P'])) +>>> data = MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])) >>> f = SongForm(data) >>> f.errors {} +The MultipleHiddenInput widget renders multiple values as hidden fields. +>>> class SongFormHidden(Form): +... name = CharField() +... composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput) +>>> f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False) +>>> print f.as_ul() +