python – 在Plone中的字段上移动不变的验证错误消息
作者:互联网
我正在使用具有灵巧性的Plone,我正在使用不变装饰器验证2个相关字段.一切正常但是…我想在一个特定字段上移动一般错误消息.
我怎样才能做到这一点?我发现马丁·阿斯佩利(Martin Aspeli)提出了一个关于如何做到这一点很酷的三年建议:
http://plone.293351.n2.nabble.com/plone-app-form-does-not-display-invariant-errors-td348710.html
但他们没有提出解决方案.
我也找到了一种方法来做到这一点,但它很难看:放置这个代码的表单的更新方法:
for widget in widgets:
name = widget.context.getName()
if errors:
for error in errors:
if isinstance(error, Invalid) and name in error.args[1:]:
if widget._error is None:
widget._error = error
是不是有一个较低级别的实现允许将字段的名称传递给凸起的Invalid,并且不需要循环遍历所有字段和每个字段的所有错误?!?
解决方法:
您可以通过在表单的操作处理程序中进行额外验证来执行此操作,并引发WidgetActionExecutionError,指定应显示错误的窗口小部件.
from five import grok
from plone.directives import form
from zope.interface import invariant, Invalid
from zope import schema
from z3c.form import button
from z3c.form.interfaces import ActionExecutionError, WidgetActionExecutionError
from Products.CMFCore.interfaces import ISiteRoot
from Products.statusmessages.interfaces import IStatusMessage
from example.dexterityforms.interfaces import MessageFactory as _
...
class OrderForm(form.SchemaForm):
...
@button.buttonAndHandler(_(u'Order'))
def handleApply(self, action):
data, errors = self.extractData()
# Some additional validation
if 'address1' in data and 'address2' in data:
if len(data['address1']) < 2 and len(data['address2']) < 2:
raise ActionExecutionError(Invalid(_(u"Please provide a valid address")))
elif len(data['address1']) < 2 and len(data['address2']) > 10:
raise WidgetActionExecutionError('address2', Invalid(u"Please put the main part of the address in the first field"))
if errors:
self.status = self.formErrorsMessage
return
我认为也可能从你的不变量引发WidgetActionExecutionError,但是如果在处理z3c.form表单时的其他时间检查不变量,它可能不会做你想要的.
标签:python,plone,validation,dexterity,z3c-form 来源: https://codeday.me/bug/20190704/1373955.html