diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 2e16c35..019f49c 100644
a
|
b
|
class MultipleHiddenInput(HiddenInput):
|
246 | 246 | A widget that handles <input type="hidden"> for fields that have a list |
247 | 247 | of values. |
248 | 248 | """ |
249 | | def __init__(self, attrs=None, choices=()): |
| 249 | def __init__(self, attrs=None, choices=(), unique_names=False): |
250 | 250 | super(MultipleHiddenInput, self).__init__(attrs) |
251 | 251 | # choices can be any iterable |
252 | 252 | self.choices = choices |
| 253 | self.unique_names = unique_names |
253 | 254 | |
254 | 255 | def render(self, name, value, attrs=None, choices=()): |
255 | 256 | if value is None: value = [] |
… |
… |
class MultipleHiddenInput(HiddenInput):
|
257 | 258 | id_ = final_attrs.get('id', None) |
258 | 259 | inputs = [] |
259 | 260 | for i, v in enumerate(value): |
| 261 | if self.unique_names: |
| 262 | final_attrs['name'] = "%s_%s" % (name, i) |
260 | 263 | input_attrs = dict(value=force_unicode(v), **final_attrs) |
261 | 264 | if id_: |
262 | 265 | # An ID attribute was given. Add a numeric index as a suffix |
diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt
index 05215d4..c337f88 100644
a
|
b
|
commonly used groups of widgets:
|
42 | 42 | |
43 | 43 | Multiple ``<input type='hidden' ...>`` widgets. |
44 | 44 | |
| 45 | Takes one optional argument: |
| 46 | |
| 47 | .. attribute:: MultipleHiddenInput.unique_names |
| 48 | |
| 49 | Determines whether the fields will have unique names |
| 50 | ``name="name_0", name="name_1", etc`` (default is ``False``). |
| 51 | |
45 | 52 | .. class:: FileInput |
46 | 53 | |
47 | 54 | File upload input: ``<input type='file' ...>`` |
diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
index b8ec789..7510302 100644
a
|
b
|
Each input gets a separate ID.
|
181 | 181 | >>> w.render('letters', list('abc'), attrs={'id': 'hideme'}) |
182 | 182 | u'<input type="hidden" name="letters" value="a" id="hideme_0" />\n<input type="hidden" name="letters" value="b" id="hideme_1" />\n<input type="hidden" name="letters" value="c" id="hideme_2" />' |
183 | 183 | |
| 184 | Each input gets a unique name. |
| 185 | >>> w = MultipleHiddenInput(unique_names=True) |
| 186 | >>> w.render('letters', list('abc'), attrs={'id': 'hideme'}) |
| 187 | u'<input type="hidden" name="letters_0" value="a" id="hideme_0" />\n<input type="hidden" name="letters_1" value="b" id="hideme_1" />\n<input type="hidden" name="letters_2" value="c" id="hideme_2" />' |
| 188 | >>> data = {'letters': ['a', 'b', 'c']} |
| 189 | >>> w.value_from_datadict(data, {}, "letters") |
| 190 | ['a', 'b', 'c'] |
| 191 | |
184 | 192 | # FileInput Widget ############################################################ |
185 | 193 | |
186 | 194 | FileInput widgets don't ever show the value, because the old value is of no use |