From 1a75b5a03caf5924b40801b34d7f3f6011ecac29 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 18 Jun 2008 16:33:04 +0000 Subject: [PATCH] Fixed #4860: added 'for' attributes to labels in newforms widgets. Thanks to Ivan Sagalaev and batiste@dosimple.ch for the patches. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7693 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/newforms/widgets.py | 16 ++++++++++++---- tests/regressiontests/forms/forms.py | 20 ++++++++++---------- tests/regressiontests/forms/regressions.py | 4 ++-- tests/regressiontests/forms/widgets.py | 16 ++++++++-------- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/AUTHORS b/AUTHORS index 861c67d10b..06e80e113a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -65,6 +65,7 @@ answer newbie questions, and generally made Django that much better: Mikaël Barbero Jiri Barton Ned Batchelder + batiste@dosimple.ch Shannon -jj Behrens Esdras Beleza Chris Bennett diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 20a7cab469..ebbf2ab727 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -281,8 +281,12 @@ class RadioInput(StrAndUnicode): self.index = index def __unicode__(self): - return mark_safe(u'' % (self.tag(), - conditional_escape(force_unicode(self.choice_label)))) + if 'id' in self.attrs: + label_for = ' for="%s_%s"' % (self.attrs['id'], self.index) + else: + label_for = '' + choice_label = conditional_escape(force_unicode(self.choice_label)) + return mark_safe(u'%s %s' % (label_for, self.tag(), choice_label)) def is_checked(self): return self.value == self.choice_value @@ -364,11 +368,15 @@ class CheckboxSelectMultiple(SelectMultiple): # so that the checkboxes don't all have the same ID attribute. if has_id: final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) + label_for = u' for="%s"' % final_attrs['id'] + else: + label_for = '' + cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) option_value = force_unicode(option_value) rendered_cb = cb.render(name, option_value) - output.append(u'
  • ' % (rendered_cb, - conditional_escape(force_unicode(option_label)))) + option_label = conditional_escape(force_unicode(option_label)) + output.append(u'
  • %s %s
  • ' % (label_for, rendered_cb, option_label)) output.append(u'') return mark_safe(u'\n'.join(output)) diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py index d7fa1780f5..7fc206de4c 100644 --- a/tests/regressiontests/forms/forms.py +++ b/tests/regressiontests/forms/forms.py @@ -443,8 +443,8 @@ zero-based index. >>> f = FrameworkForm(auto_id='id_%s') >>> print f['language']
      -
    • -
    • +
    • +
    When RadioSelect is used with auto_id, and the whole form is printed using @@ -453,20 +453,20 @@ ID of the *first* radio button. >>> print f
      -
    • -
    • +
    • +
    >>> print f.as_ul()
    • -
    • -
    • +
    • +
  • >>> print f.as_p()

      -
    • -
    • +
    • +

    MultipleChoiceField is a special case, as its data is required to be a list: @@ -535,8 +535,8 @@ zero-based index. >>> f = SongForm(auto_id='%s_id') >>> print f['composers']
      -
    • -
    • +
    • +
    Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict diff --git a/tests/regressiontests/forms/regressions.py b/tests/regressiontests/forms/regressions.py index 1bb6f6e7e5..cbc8095e60 100644 --- a/tests/regressiontests/forms/regressions.py +++ b/tests/regressiontests/forms/regressions.py @@ -40,7 +40,7 @@ Unicode decoding problems... ... somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf') >>> f = SomeForm() >>> f.as_p() -u'

      \n
    • \n
    • \n
    • \n

    ' +u'

      \n
    • \n
    • \n
    • \n

    ' Testing choice validation with UTF-8 bytestrings as input (these are the Russian abbreviations "мес." and "шт.". @@ -56,7 +56,7 @@ Translated error messages used to be buggy. >>> activate('ru') >>> f = SomeForm({}) >>> f.as_p() -u'
    • \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.
    \n

      \n
    • \n
    • \n
    • \n

    ' +u'
    • \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.
    \n

      \n
    • \n
    • \n
    • \n

    ' >>> deactivate() Deep copying translated text shouldn't raise an error diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index c83fe40928..2c6b51a8ec 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -741,20 +741,20 @@ u'
      \n
    • -
    • -
    • -
    • +
    • +
    • +
    • +
    # 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 ###############################################