2013-02-28 84 views
2

我有一個靈巧型,具有圖像字段定義是這樣的:圖片字段驗證爲特定的寬度/高度

image = NamedBlobImage(
    title=_(u'Lead Image'), 
    description=_(u"Upload a Image of Size 230x230."), 
    required=True, 
) 

我如何添加一個驗證器來檢查上傳的圖片文件?例如,如果圖像寬度超過500像素,則警告用戶上傳另一個文件。提示或示例代碼表示讚賞。

回答

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")) 

然後設置功能您NamedBlobImageconstraint

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

相關問題