1. btparfum
  2. wtforms


JTG  committed f1989d9

Add _class kwarg to Field.__init__ (same as label, id etc.)

  • Participants
  • Parent commits 396390d
  • Branches default

Comments (0)

Files changed (1)

File wtforms/fields/core.py

View file
     def __init__(self, label=None, validators=None, filters=tuple(),
                  description='', id=None, default=None, widget=None,
-                 _form=None, _name=None, _prefix='', _translations=None):
+                 _form=None, _name=None, _prefix='', _translations=None, _class=None):
         Construct a new field.
         :param _prefix:
             The prefix to prepend to the form name of this field, passed by
             the enclosing form during construction.
+        :param _class:
+            A class/classes to use for the field. Pass value here, if you can't
+            set attribute in __call__ later (e.g. using WTForms in frameworks).
         If `_form` and `_name` isn't provided, an :class:`UnboundField` will be
         returned instead. Call its :func:`bind` method with a form instance and
         self.id = id or self.name
         self.label = Label(self.id, label if label is not None else self.gettext(_name.replace('_', ' ').title()))
+        self._class = _class
         if widget is not None:
             self.widget = widget
         Any HTML attribute passed to the method will be added to the tag
         and entity-escaped properly.
+        if self._class:
+            kwargs['class_'] = kwargs['class_'] + " " + self._class if "class_" in kwargs else self._class        
         return self.widget(self, **kwargs)
     def gettext(self, string):