plonez3c.form

How to customize z3c.form widget for fields inside groups (fieldsets)


I have a Plone custom control panel registry and I'm trying to use a well know method to customize some of the widgets properties for zope.schema.Text and zope.schema.TextField.

I commonly customize the updateWidgets in that way:

def updateWidgets(self):
    super(MyEditForm, self).updateWidgets()
    self.widgets['my_text_area'].style = 'width: 100%'
    self.widgets['my_text_area'].rows = 7

But now I'm working on a form where fields are splitted in two fieldsets:

class MySettingsEditForm(controlpanel.RegistryEditForm):
    schema = IMySettingsSchema
    groups = (Form1, Form2)
    # fields = nothing

If I try to access self.widgets['my_text_area'] I get KeyError. It seems that as I did't defined the fields attribute I can't access directly widgets.

I found that I have groups so I can call something like self.groups[0].fields['my_text_area'] but still I find no way to access widgets for fields inside groups.

How can I customize widgets attributes when using groups?


Solution

  • I think what you need is playing with widget subform, see this code:

    def fix_table_widget(self, name, widgets):
        sub_widgets = widgets[name].widgets
        for widget in sub_widgets:
            new_label = widget.subform.widgets['weekday'].value
            widget.subform.widgets['selected'].items[0]['label'] = new_label
            widget.subform.widgets['weekday'].mode = 'hidden'
    
    def schoolrequest_customizations(self):
        ''' Customizations for the schoolrequest base views
        '''
        for group in self.groups:
            widgets = group.widgets
            if 'table_bus_to_school' in widgets:
                self.block_widget_table('table_bus_to_school', widgets)
                self.fix_table_widget('table_bus_to_school', widgets)
    
            if 'table_bus_to_home' in widgets:
                self.block_widget_table('table_bus_to_home', widgets)
                self.fix_table_widget('table_bus_to_home', widgets)
    
    def update(self):
        super(MyForm, self).update()
        self.schoolrequest_customizations()