Ticket #11836: multiplehiddeninput_unique_names.diff

File multiplehiddeninput_unique_names.diff, 2.8 KB (added by brianmacdonald, 14 years ago)
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 2e16c35..019f49c 100644
    a b class MultipleHiddenInput(HiddenInput):  
    246246    A widget that handles <input type="hidden"> for fields that have a list
    247247    of values.
    248248    """
    249     def __init__(self, attrs=None, choices=()):
     249    def __init__(self, attrs=None, choices=(), unique_names=False):
    250250        super(MultipleHiddenInput, self).__init__(attrs)
    251251        # choices can be any iterable
    252252        self.choices = choices
     253        self.unique_names = unique_names
    253254
    254255    def render(self, name, value, attrs=None, choices=()):
    255256        if value is None: value = []
    class MultipleHiddenInput(HiddenInput):  
    257258        id_ = final_attrs.get('id', None)
    258259        inputs = []
    259260        for i, v in enumerate(value):
     261            if self.unique_names:
     262                final_attrs['name'] = "%s_%s" % (name, i)
    260263            input_attrs = dict(value=force_unicode(v), **final_attrs)
    261264            if id_:
    262265                # An ID attribute was given. Add a numeric index as a suffix
  • docs/ref/forms/widgets.txt

    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:  
    4242
    4343    Multiple ``<input type='hidden' ...>`` widgets.
    4444
     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
    4552.. class:: FileInput
    4653
    4754    File upload input: ``<input type='file' ...>``
  • tests/regressiontests/forms/widgets.py

    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.  
    181181>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
    182182u'<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" />'
    183183
     184Each input gets a unique name.
     185>>> w = MultipleHiddenInput(unique_names=True)
     186>>> w.render('letters', list('abc'), attrs={'id': 'hideme'})
     187u'<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
    184192# FileInput Widget ############################################################
    185193
    186194FileInput widgets don't ever show the value, because the old value is of no use
Back to Top