Menu

← Back to all posts

Tech

Convert z3c.form field desc. to tooltip

Let's say you have a typical form with some input fields. By default z3c.form displays a description (if provided in form definition) above each form field,...

Peter LamutPeter Lamut
# file eventlist.py in browser directory
...
from z3c.form import form
...

class ListEventsForm(form.Form):
    ...

    def updateWidgets(self):
        """Move fields' descriptions to title attributes of HTML form elements."""
        super(ListEventsForm, self).updateWidgets()
        for name, widget in self.widgets.items():
            widget.title = widget.field.description

In the ListEventsForm class we override the updateWidgets method in order to iterate through all form fields and set their title attribute. Note that this makes tooltips to show, but field descriptions themselves are still visible. We hide them with the following CSS rule (eventlist-form being our form’s ID):

#eventlist-form div.formHelp {
    display: none;
}

And that’s all, folks!

Peter Lamut

Peter Lamut

Peter was one of the original co-founders of Niteo and the main developer on our early projects.