DX: Set class on form field?

How do I set a class on the outer div of a form field w/o redefining the widget (iow: w/o bloating my code)?

example: I have

name = schema.TextLine(
    title=u'name',
    required=True,
)

I want something like:

name = schema.TextLine(
    ...
    class="my-special-klass"
    ...
)

or

name = schema.TextLine(
    ...
)
addClass('name', "my-special-klass")

Anything I've overseen?

have you tried plone.autoform directives? I think you can use something like:

from plone.autoform import directives as form
from plone.supermodel import model

....
class IMySchema(model.Schema):
    form.widget('name', klass='my-special-klass')
    name = schema.TextLine(
        ...
    )
1 Like

@hvelarde Thanks, but it sets the class on the inner input and not on the outer div.

@jensens: Did you find a solution (or work-around) for this? I currently have the same problem and don't know how to solve it.

I think you need to customize p.a.z3cform 's widget.pt. The readme file has some info on how to do it.

I Just found that

from plone.autoform import directives
...
directives.widget('field_name', wrapper_css_class='my-special-klass')

set a class on the outer div of a specific form field.

2 Likes

Can't you use the :has() pseudo class?

Great find! That should be added to the docs.