Ticket #6270: label_attrs.diff

File label_attrs.diff, 3.1 KB (added by David Tulig, 17 years ago)

Adds "label_attrs" to the Field constructor to define attributes for the label tag

  • django/newforms/fields.py

    diff --git a/django/newforms/fields.py b/django/newforms/fields.py
    index 3b8f419..f3f6a70 100644
    a b class Field(object):  
    5050    creation_counter = 0
    5151
    5252    def __init__(self, required=True, widget=None, label=None, initial=None,
    53                  help_text=None, error_messages=None):
     53                 help_text=None, error_messages=None, label_attrs=None):
    5454        # required -- Boolean that specifies whether the field is required.
    5555        #             True by default.
    5656        # widget -- A Widget class, or instance of a Widget class, that should
    class Field(object):  
    6464        # initial -- A value to use in this Field's initial display. This value
    6565        #            is *not* used as a fallback if data isn't given.
    6666        # help_text -- An optional string to use as "help text" for this Field.
     67        # label_attrs -- An optional dictionary to use as attributes for the
     68        #                label html tag.
    6769        if label is not None:
    6870            label = smart_unicode(label)
    69         self.required, self.label, self.initial = required, label, initial
     71        self.required, self.initial = required, initial
     72        self.label, self.label_attrs = label, label_attrs
    7073        self.help_text = smart_unicode(help_text or '')
    7174        widget = widget or self.widget
    7275        if isinstance(widget, type):
  • django/newforms/forms.py

    diff --git a/django/newforms/forms.py b/django/newforms/forms.py
    index 556c00a..8d8c48a 100644
    a b class BaseForm(StrAndUnicode):  
    124124                    if self.label_suffix:
    125125                        if label[-1] not in ':?.!':
    126126                            label += self.label_suffix
    127                     label = bf.label_tag(label) or ''
     127                    label = bf.label_tag(label, bf.label_attrs) or ''
    128128                else:
    129129                    label = ''
    130130                if field.help_text:
    class BoundField(StrAndUnicode):  
    238238        else:
    239239            self.label = self.field.label
    240240        self.help_text = field.help_text or ''
     241        self.label_attrs = self.field.label_attrs
    241242
    242243    def __unicode__(self):
    243244        """Renders this field as an HTML widget."""
  • docs/newforms.txt

    diff --git a/docs/newforms.txt b/docs/newforms.txt
    index 19f42cb..d044884 100644
    a b And here is a custom error message::  
    11281128In the `built-in Field classes`_ section below, each ``Field`` defines the
    11291129error message keys it uses.
    11301130
     1131``label_attrs``
     1132~~~~~~~~~~~~~~~~~~
     1133
     1134**New in Django development version**
     1135
     1136The ``label_attrs`` argument lets you define the extra arguments that the
     1137label tag will contain. Pass in a dictionary with keys matching the attribute
     1138you want to define. For example, here is a way to define the label's class::
     1139
     1140    >>> generic = forms.CharField(label_attrs={'class': 'large_label'})
     1141
     1142And here is the output::
     1143
     1144    <label for="id_generic" class="large_label">Generic:</label>
     1145
    11311146Dynamic initial values
    11321147----------------------
    11331148
Back to Top