Opened 6 years ago

Last modified 6 years ago

#29153 closed New feature

add label attrs to BaseForm — at Version 2

Reported by: Christoph Owned by: nobody
Component: Forms Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Christoph)

Goal:
Add attributes like "class" to the label tag for inputs.

Problem:
Actually the only thing thats stops me from using {{ form }} is the incapability to change classes for labels easily. So i have to do it like this, for every form and every field:

{{ form.non_field_errors }}
<div class="fieldWrapper">
    {{ form.subject.errors }}
    <label for="{{ form.subject.id_for_label }}" class="MY-CUSTOM-CLASS">Email subject:</label>
    {{ form.subject }}
</div>

Where it should be changed:
I would be fine if the label_attrs could be set in BaseForm(), but i think the right place would be class Field

As the label_tag is already set up to give the label class attributes and label_suffix, it is quite easy to add this to the BaseForm _html_output()

class BoundField:
    def label_tag(self, contents=None, attrs=None, label_suffix=None):
class BaseForm:
    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
        # ...
        label = bf.label_tag(label) or ''

Change History (2)

comment:1 by Tim Graham, 6 years ago

I don't understand what change you're proposing. Can you provide a patch or describe the changes and the use case?

comment:2 by Christoph, 6 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top