我有一个 z3c.form 在执行表单操作之前无法知道一些错误。我想在字段上显示这些错误,而不是在全局表单状态消息中。Form.update() 中的小部件如何构造和注入错误?
例子:
@z3c.form.button.buttonAndHandler(_('Make Report'), name='report')
def report(self, action):
data, errors = self.extractData()
if errors:
self.status = "Please correct errors"
return
# Create sample item which we can consume in the page template
try:
self.output = make_report(self.context, self.request, data, filters=filters)
except zope.interface.Invalid as e:
self.status = e.message
self.errors = True
# How to target the error message to a particular field here
return
self.status = _(u"Report complete")