Ticket #9079: label_tag_r9027.diff

File label_tag_r9027.diff, 1.9 KB (added by oggy, 16 years ago)
  • django/forms/fields.py

     
    5959    creation_counter = 0
    6060
    6161    def __init__(self, required=True, widget=None, label=None, initial=None,
    62                  help_text=None, error_messages=None, show_hidden_initial=False):
     62                 help_text=None, error_messages=None, show_hidden_initial=False, label_tag_attrs=None):
    6363        # required -- Boolean that specifies whether the field is required.
    6464        #             True by default.
    6565        # widget -- A Widget class, or instance of a Widget class, that should
     
    108108        messages.update(error_messages or {})
    109109        self.error_messages = messages
    110110
     111        self.label_tag_attrs = {}
     112        self.label_tag_attrs.update(label_tag_attrs or {})
     113
    111114    def clean(self, value):
    112115        """
    113116        Validates the given value and returns its "cleaned" value as an
  • django/forms/forms.py

     
    389389        If attrs are given, they're used as HTML attributes on the <label> tag.
    390390        """
    391391        contents = contents or escape(self.label)
     392        tag_attrs = {}
     393        tag_attrs.update(self.field.label_tag_attrs)
     394        tag_attrs.update(attrs or {})
    392395        widget = self.field.widget
    393396        id_ = widget.attrs.get('id') or self.auto_id
    394397        if id_:
    395             attrs = attrs and flatatt(attrs) or ''
    396             contents = u'<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, unicode(contents))
     398            tag_attrs = tag_attrs and flatatt(tag_attrs) or ''
     399            contents = u'<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), tag_attrs, unicode(contents))
    397400        return mark_safe(contents)
    398401
    399402    def _is_hidden(self):
Back to Top