Opened 7 years ago

Closed 4 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@…> 7 years ago.
handle None values from id_for_label
django_label_for_none_incl_tests.patch (2.6 KB) - added by DenisMartinez 7 years ago.
Same patch applied to svn r13303, including tests

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by Denis Martinez <deuns.martinez@…>

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

handle None values from id_for_label

comment:1 Changed 7 years ago by Chris Beaven

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 Changed 7 years ago by DenisMartinez

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 Changed 7 years ago by Alex Gaynor

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

Changed 7 years ago by DenisMartinez

Same patch applied to svn r13303, including tests

comment:4 Changed 6 years ago by Peter Baumgartner

Severity: Normal
Type: New feature

comment:5 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:7 Changed 4 years ago by Sergey Kolosov

Owner: changed from nobody to Sergey Kolosov
Status: newassigned

comment:8 Changed 4 years ago by Sergey Kolosov

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 Changed 4 years ago by Erik Romijn

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 Changed 4 years ago by Claude Paroz

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