如何为组内的字段自定义 z3c.form 小部件(字段集)
How to customize z3c.form widget for fields inside groups (fieldsets)
我有一个 Plone 自定义控制面板注册表,我正在尝试使用众所周知的方法为 zope.schema.Text
和 zope.schema.TextField
自定义一些小部件属性。
我通常以这种方式自定义 updateWidgets
:
def updateWidgets(self):
super(MyEditForm, self).updateWidgets()
self.widgets['my_text_area'].style = 'width: 100%'
self.widgets['my_text_area'].rows = 7
但现在我正在处理一个字段被分成两个字段集的表单:
class MySettingsEditForm(controlpanel.RegistryEditForm):
schema = IMySettingsSchema
groups = (Form1, Form2)
# fields = nothing
如果我尝试访问 self.widgets['my_text_area']
,我会得到 KeyError
。似乎因为我没有定义 fields
属性,所以我无法直接访问小部件。
我发现我有 groups
,所以我可以调用类似 self.groups[0].fields['my_text_area']
的东西,但我仍然找不到访问组内字段的小部件的方法。
如何在使用组时自定义小部件属性?
我想你需要的是玩小部件子表单,看这段代码:
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()
我有一个 Plone 自定义控制面板注册表,我正在尝试使用众所周知的方法为 zope.schema.Text
和 zope.schema.TextField
自定义一些小部件属性。
我通常以这种方式自定义 updateWidgets
:
def updateWidgets(self):
super(MyEditForm, self).updateWidgets()
self.widgets['my_text_area'].style = 'width: 100%'
self.widgets['my_text_area'].rows = 7
但现在我正在处理一个字段被分成两个字段集的表单:
class MySettingsEditForm(controlpanel.RegistryEditForm):
schema = IMySettingsSchema
groups = (Form1, Form2)
# fields = nothing
如果我尝试访问 self.widgets['my_text_area']
,我会得到 KeyError
。似乎因为我没有定义 fields
属性,所以我无法直接访问小部件。
我发现我有 groups
,所以我可以调用类似 self.groups[0].fields['my_text_area']
的东西,但我仍然找不到访问组内字段的小部件的方法。
如何在使用组时自定义小部件属性?
我想你需要的是玩小部件子表单,看这段代码:
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()