Ticket #3515: fields.3.diff

File fields.3.diff, 1.6 KB (added by Simon Litchfield <simon@…>, 17 years ago)

Use this one

  • django/newforms/fields.py

     
    3535    # Tracks each time a Field instance is created. Used to retain order.
    3636    creation_counter = 0
    3737
    38     def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None):
     38    def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None, css=None):
    3939        # required -- Boolean that specifies whether the field is required.
    4040        #             True by default.
    4141        # widget -- A Widget class, or instance of a Widget class, that should be
     
    4848        # initial -- A value to use in this Field's initial display. This value is
    4949        #            *not* used as a fallback if data isn't given.
    5050        # help_text -- An optional string to use as "help text" for this Field.
     51        # css -- An optional string to set the CSS class name(s) for this Field.
    5152        if label is not None:
    5253            label = smart_unicode(label)
    5354        self.required, self.label, self.initial = required, label, initial
     
    6061        extra_attrs = self.widget_attrs(widget)
    6162        if extra_attrs:
    6263            widget.attrs.update(extra_attrs)
     64
     65        # Set initial CSS class name(s)
     66        if widget.attrs.has_key('class'):
     67            widget.attrs.remove('class')
     68        if css or required:
     69            if required: css = (css or '') + 'required'
     70            widget.attrs.update({'class': css.lstrip()})
    6371
    6472        self.widget = widget
Back to Top