Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3289 -- newforms: Added value_from_datadict method to Multiple…

…HiddenInput

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4311 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a0137c41f3b9b95d48f42ddd0fb48703a88a8c94 1 parent 663ef14
Adrian Holovaty authored January 13, 2007
12  django/newforms/widgets.py
@@ -92,11 +92,21 @@ class MultipleHiddenInput(HiddenInput):
92 92
     A widget that handles <input type="hidden"> for fields that have a list
93 93
     of values.
94 94
     """
95  
-    def render(self, name, value, attrs=None):
  95
+    def __init__(self, attrs=None, choices=()):
  96
+        # choices can be any iterable
  97
+        self.attrs = attrs or {}
  98
+        self.choices = choices
  99
+
  100
+    def render(self, name, value, attrs=None, choices=()):
96 101
         if value is None: value = []
97 102
         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
98 103
         return u'\n'.join([(u'<input%s />' % flatatt(dict(value=smart_unicode(v), **final_attrs))) for v in value])
99 104
 
  105
+    def value_from_datadict(self, data, name):
  106
+        if isinstance(data, MultiValueDict):
  107
+            return data.getlist(name)
  108
+        return data.get(name, None)
  109
+
100 110
 class FileInput(Input):
101 111
     input_type = 'file'
102 112
 
11  tests/regressiontests/forms/tests.py
@@ -1950,11 +1950,20 @@
1950 1950
 >>> f.errors
1951 1951
 {}
1952 1952
 >>> from django.utils.datastructures import MultiValueDict
1953  
->>> data = MultiValueDict(dict(name='Yesterday', composers=['J', 'P']))
  1953
+>>> data = MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P']))
1954 1954
 >>> f = SongForm(data)
1955 1955
 >>> f.errors
1956 1956
 {}
1957 1957
 
  1958
+The MultipleHiddenInput widget renders multiple values as hidden fields.
  1959
+>>> class SongFormHidden(Form):
  1960
+...     name = CharField()
  1961
+...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput)
  1962
+>>> f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False)
  1963
+>>> print f.as_ul()
  1964
+<li>Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" />
  1965
+<input type="hidden" name="composers" value="P" /></li>
  1966
+
1958 1967
 When using CheckboxSelectMultiple, the framework expects a list of input and
1959 1968
 returns a list of input.
1960 1969
 >>> f = SongForm({'name': 'Yesterday'}, auto_id=False)

0 notes on commit a0137c4

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