Opened 15 years ago

Closed 12 years ago

#11725 closed New feature (fixed)

Unable to have label tags without a "for" attribute in form widgets

Reported by: Denis Martinez <deuns.martinez@…> Owned by: Sergey Kolosov
Component: Forms Version: 1.1
Severity: Normal Keywords: forms label for none dceu13
Cc: eromijn@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In some cases I want to use a form widget without an input, such as pure javascript or iframe
(recaptcha_django in my case).

form.as_p() generates a label tag with an unmatched id: <label for="id_recaptcha"> .

This makes my page invalid in HTML5.

I tried to override id_for_label like this in my widget's code:

    def id_for_label(self, id_):
        return None

which gives <label for="None"> .

Anyway, I believe that this case should be treated adequately, because the docstring
states that id_for_label can return None values.

Attachments (2)

django-label-for-none.patch (848 bytes ) - added by Denis Martinez <deuns.martinez@…> 15 years ago.
handle None values from id_for_label
django_label_for_none_incl_tests.patch (2.6 KB ) - added by DenisMartinez 15 years ago.
Same patch applied to svn r13303, including tests

Download all attachments as: .zip

Change History (12)

by Denis Martinez <deuns.martinez@…>, 15 years ago

Attachment: django-label-for-none.patch added

handle None values from id_for_label

comment:1 by Chris Beaven, 15 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by DenisMartinez, 15 years ago

This patch has been accepted since Django 1.0, so can it be included mainline ?

I don't want to have to apply this on every release.

comment:3 by Alex Gaynor, 15 years ago

The patch is clearly missing tests, which are not optional.

by DenisMartinez, 15 years ago

Same patch applied to svn r13303, including tests

comment:4 by Peter Baumgartner, 14 years ago

Severity: Normal
Type: New feature

comment:5 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 by Sergey Kolosov, 12 years ago

Owner: changed from nobody to Sergey Kolosov
Status: newassigned

comment:8 by Sergey Kolosov, 12 years ago

Needs tests: unset

Pull request: https://github.com/django/django/pull/1179

I had to reject tests from the patch above (those were doctests, and those tested a complete form rather than just a label tag).

comment:9 by Sasha Romijn, 12 years ago

Cc: eromijn@… added
Keywords: dceu13 added
Triage Stage: AcceptedReady for checkin

Looks fine to me. Tests work. I'd like to see it implemented slightly simpler, but because we also don't want a dangling for="" in the code when there is no ID, I think this is as clean as it gets.

comment:10 by Claude Paroz, 12 years ago

Resolution: fixed
Status: assignedclosed

In be0bab1bb8da80402248cd1fa22fd4cc09b34fe7:

Fixed #11725 -- Made possible to create widget label tag without "for"

Thanks Denis Martinez for the report and initial patch, and
Sergey Kolosov for bringing the patch up to date.
Note: See TracTickets for help on using tickets.
Back to Top