我有一个灵巧类型,图像字段定义如下所示:
image = NamedBlobImage(
title=_(u'Lead Image'),
description=_(u"Upload a Image of Size 230x230."),
required=True,
)
如何添加验证器来检查上传的图像文件?例如,如果图像宽度超过 500 像素,则警告用户上传另一个文件。提示或示例代码表示赞赏。
你想设置一个约束函数:
from zope.interface import Invalid
from foo.bar import MessageFactory as _
def imageSizeConstraint(value):
# value implements the plone.namedfile.interfaces.INamedBlobImageField interface
width, height = value.getImageSize()
if width > 500 or height > 500:
raise Invalid(_(u"Your image is too large"))
然后将该功能设置constraint
为您的NamedBlobImage
字段:
image = NamedBlobImage(
title=_(u'Lead Image'),
description=_(u"Upload a Image of Size 230x230."),
constraint=imageSizeConstraint,
required=True,
)
有关更多信息以及接口定义,请参阅有关验证的敏捷性手册。plone.namedfile