From 56b8914eb968ad49d3a15a98cec65fec849e1227 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 24 Apr 2007 12:53:29 +0000 Subject: [PATCH] Fixed #3870, Refs #3787 -- Fixed handling of widget attributes on RadioSelect and MultiWidget. In particular, handling of the id attribute has been fixed. Thanks to Gary Wilson and Max Derkachev. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5065 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/widgets.py | 19 +++++++++++++++---- tests/regressiontests/forms/tests.py | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index f701faa35df..d50b1921ea3 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -260,8 +260,8 @@ class RadioSelect(Select): "Returns a RadioFieldRenderer instance rather than a Unicode string." if value is None: value = '' str_value = smart_unicode(value) # Normalize to string. - attrs = attrs or {} - return RadioFieldRenderer(name, str_value, attrs, list(chain(self.choices, choices))) + final_attrs = self.build_attrs(attrs) + return RadioFieldRenderer(name, str_value, final_attrs, list(chain(self.choices, choices))) def id_for_label(self, id_): # RadioSelect is represented by multiple fields, @@ -327,14 +327,25 @@ class MultiWidget(Widget): if not isinstance(value, list): value = self.decompress(value) output = [] + final_attrs = self.build_attrs(attrs) + id_ = final_attrs.get('id', None) for i, widget in enumerate(self.widgets): try: widget_value = value[i] - except KeyError: + except IndexError: widget_value = None - output.append(widget.render(name + '_%s' % i, widget_value, attrs)) + if id_: + final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) + output.append(widget.render(name + '_%s' % i, widget_value, final_attrs)) return self.format_output(output) + def id_for_label(self, id_): + # See the comment for RadioSelect.id_for_label() + if id_: + id_ += '_0' + return id_ + id_for_label = classmethod(id_for_label) + def value_from_datadict(self, data, name): return [data.get(name + '_%s' % i) for i in range(len(self.widgets))] diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 4521d17d7f0..8f8b3c828ad 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -658,10 +658,31 @@ Traceback (most recent call last): ... IndexError: list index out of range +# Unicode choices are correctly rendered as HTML >>> w = RadioSelect() >>> unicode(w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])) u'' +# Attributes provided at instantiation are passed to the constituent inputs +>>> w = RadioSelect(attrs={'id':'foo'}) +>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) + + +# Attributes provided at render-time are passed to the constituent inputs +>>> w = RadioSelect() +>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}) + + # CheckboxSelectMultiple Widget ############################################### >>> w = CheckboxSelectMultiple() @@ -783,6 +804,11 @@ u'