python – 在Plone中的字段上移动不变的验证错误消息
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在Plone中的字段上移动不变的验证错误消息,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2078字,纯文字阅读大概需要3分钟。
内容图文
![python – 在Plone中的字段上移动不变的验证错误消息](/upload/InfoBanner/zyjiaocheng/777/8c06316d8989407093c28e40f627e625.jpg)
我正在使用具有灵巧性的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中的字段上移动不变的验证错误消息全部内容,希望文章能够帮你解决python – 在Plone中的字段上移动不变的验证错误消息所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。