Ticket #4860: 4860.labels-with-fors.diff

File 4860.labels-with-fors.diff, 10.3 KB (added by Ivan Sagalaev <Maniac@…>, 17 years ago)

Patch with labels and proper fors

  • django/newforms/widgets.py

     
    281281        self.index = index
    282282
    283283    def __unicode__(self):
    284         return mark_safe(u'<label>%s %s</label>' % (self.tag(),
     284        if 'id' in self.attrs:
     285            for_attr = ' for="%s_%s"' % (self.attrs['id'], self.index)
     286        else:
     287            for_attr = ''
     288        return mark_safe(u'<label%s>%s %s</label>' % (for_attr, self.tag(),
    285289                conditional_escape(force_unicode(self.choice_label))))
    286290
    287291    def is_checked(self):
    288292        return self.value == self.choice_value
    289293
    290294    def tag(self):
     295        final_attrs = dict(self.attrs, type='radio', name=self.name, value=self.choice_value)
    291296        if 'id' in self.attrs:
    292             self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
    293         final_attrs = dict(self.attrs, type='radio', name=self.name, value=self.choice_value)
     297            final_attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
    294298        if self.is_checked():
    295299            final_attrs['checked'] = 'checked'
    296300        return mark_safe(u'<input%s />' % flatatt(final_attrs))
     
    364368            # so that the checkboxes don't all have the same ID attribute.
    365369            if has_id:
    366370                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
     371                for_attr = ' for="%s_%s"' % (attrs['id'], i)
     372            else:
     373                for_attr = ''
    367374            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
    368375            option_value = force_unicode(option_value)
    369376            rendered_cb = cb.render(name, option_value)
    370             output.append(u'<li><label>%s %s</label></li>' % (rendered_cb,
     377            output.append(u'<li><label%s>%s %s</label></li>' % (for_attr, rendered_cb,
    371378                    conditional_escape(force_unicode(option_label))))
    372379        output.append(u'</ul>')
    373380        return mark_safe(u'\n'.join(output))
  • tests/regressiontests/forms/regressions.py

     
    4040...     somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf')
    4141>>> f = SomeForm()
    4242>>> f.as_p()
    43 u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
     43u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
    4444
    4545Testing choice validation with UTF-8 bytestrings as input (these are the
    4646Russian abbreviations "мес." and "шт.".
     
    5656>>> activate('ru')
    5757>>> f = SomeForm({})
    5858>>> f.as_p()
    59 u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
     59u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
    6060>>> deactivate()
    6161
    6262Deep copying translated text shouldn't raise an error
  • tests/regressiontests/forms/forms.py

     
    424424>>> f = FrameworkForm(auto_id='id_%s')
    425425>>> print f['language']
    426426<ul>
    427 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
    428 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
     427<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
     428<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
    429429</ul>
    430430
    431431When RadioSelect is used with auto_id, and the whole form is printed using
     
    434434>>> print f
    435435<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>
    436436<tr><th><label for="id_language_0">Language:</label></th><td><ul>
    437 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
    438 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
     437<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
     438<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
    439439</ul></td></tr>
    440440>>> print f.as_ul()
    441441<li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
    442442<li><label for="id_language_0">Language:</label> <ul>
    443 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
    444 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
     443<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
     444<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
    445445</ul></li>
    446446>>> print f.as_p()
    447447<p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
    448448<p><label for="id_language_0">Language:</label> <ul>
    449 <li><label><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
    450 <li><label><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
     449<li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
     450<li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
    451451</ul></p>
    452452
    453453MultipleChoiceField is a special case, as its data is required to be a list:
     
    516516>>> f = SongForm(auto_id='%s_id')
    517517>>> print f['composers']
    518518<ul>
    519 <li><label><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
    520 <li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
     519<li><label for="composers_id_0"><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
     520<li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
    521521</ul>
    522522
    523523Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict
  • tests/regressiontests/forms/widgets.py

     
    741741>>> w = RadioSelect(attrs={'id':'foo'})
    742742>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    743743<ul>
    744 <li><label><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
    745 <li><label><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
    746 <li><label><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
    747 <li><label><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
     744<li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
     745<li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
     746<li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
     747<li><label for="foo_3"><input type="radio" id="foo_3" value="R" name="beatle" /> Ringo</label></li>
    748748</ul>
    749749
    750750# Attributes provided at render-time are passed to the constituent inputs
    751751>>> w = RadioSelect()
    752752>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
    753753<ul>
    754 <li><label><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
    755 <li><label><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
    756 <li><label><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
    757 <li><label><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
     754<li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
     755<li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
     756<li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>
     757<li><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></li>
    758758</ul>
    759759
    760760# CheckboxSelectMultiple Widget ###############################################
Back to Top