我有一个灵巧类型,图像字段定义如下:
image = NamedBlobImage(
title=_(u'Lead Image'),
description=_(u"Upload a Image of Size 230x230."),
required=True,
)
如何添加验证器以检查上传的图像文件?例如,如果图像宽度超过500像素,则警告用户上传另一个文件。提示或示例代码表示赞赏。
答案 0 :(得分:4)
您想设置约束函数:
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,
)
有关详情,请参阅Dexterity manual on validation以及plone.namedfile
interface definitions。