我如何修复架构中的 Dexterity Plone 4 错误?

How I Fix in Dexterity Plone 4 error in Schema?

我正在为 Plone 4 中的 Dexterity 编写这段代码,代码很简单,但是它会导致一定的错误,下面我们看到代码和错误的图像,我在那里解释我做了什么。

from plone.autoform import directives
from plone.namedfile import field as namedfile
from plone.supermodel import model
from z3c.form.browser.radio import RadioFieldWidget
from zope import schema
from zope.interface import invariant, Invalid
from zope.schema.vocabulary import SimpleVocabulary
from zope.schema.vocabulary import SimpleTerm
from datetime import datetime

from projetime.ged import _

VocabularyDocumentType = SimpleVocabulary(
    [SimpleTerm(value=u'lawsuits', title=_(u'Lawsuits')),
     SimpleTerm(value=u'contracts', title=_(u'Contracts')),
     SimpleTerm(value=u'another', title=_(u'Another Docs'))]
)

def nowDateTime():
    return datetime.today()

class IDigitalFile(model.Schema):
    """Dexterity-Schema
    """

    directives.widget(documentType=RadioFieldWidget)
    documentType = schema.Choice(
        title=_(u"Document Type"),
        vocabulary=VocabularyDocumentType,
        required=True,
    )

    documentCod = schema.TextLine(
        title=_(u"Document Cod."),
        description=_(u"The Document Cod must be have xxxx/yyyy"),
        required=False,
    )

    identification = schema.TextLine(
        title=_(u"Identification"),
        description=_(u"Enter your indetification"),
        min_length = 11,
        required=False,
    )

    subject = schema.TextLine(
        title=_(u"Subject"),
        required=True,
    )

    typeOf = schema.TextLine(
        title=_(u"Type of Document"),
        required=False,
    )

    file = namedfile.NamedBlobFile(
        title=_(u"Digital Archive"),
        required=True,
    )

    directives.mode(auto="hidden")
    auto = schema.Bool(
        title=_(u"Upload via Script?"),
        required=True,
        default=False,
    )


    @invariant
    def DocumentTypeInvariant(data):
        if data.documentType == 'lawsuits':
            if not data.documentCod or not data.identification or not data.typeOf or not data.Description:
                raise Invalid(_(u"You choose Lawsuits, all fields are required!"))
        elif data.documentType == 'contracts':
            if not data.documentCod or not data.identification or not data.Description:
                raise Invalid(_(u"You choose Contracts, All fields with EXCEPTION of Type of Document are required"))

当我将在 Plone 中创建名为 DigitalFile 的对象时,我开始填写字段,当所有字段都已填写时,在单击提交之前出现此错误:

我该如何解决?

invariant 是在 schema 中定义的,而 Description 字段在此架构中是 而不是 ,因此您无法访问它不变量。

您需要考虑到 Description 字段是在不同的架构中定义的。

您可以使用 Form validator 解决您的问题: http://docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers

为此,您需要注册一个自定义 edit/add 表单: 检查:http://docs.plone.org/external/plone.app.dexterity/docs/advanced/custom-add-and-edit-forms.html

对于克隆 4 和克隆 5,这一切都有很好的记录。