【问题标题】:How I Fix in Dexterity Plone 4 error in Schema?如何修复架构中的 Dexterity Plone 4 错误?
【发布时间】:2016-12-20 17:10:44
【问题描述】:

我正在为 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 的对象时,我开始填写字段,当所有字段都填写完毕后,点击提交前出现此错误:

我该如何解决?

【问题讨论】:

    标签: python schema plone dexterity plone-4.x


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我假设IDublinCore.description 在上面的代码中,它是由data.Description 调用的,就像我们在页面模板中所做的那样。在这种情况下,我想在选择 lawsuitscontracts 时进行所需的描述。
    • 您在 Form-validator 中获得了所有表单数据,但您有一个不变量,它被固定在您定义的模式中。不变量中的data 与表单验证器中的data 不同。您必须使用表单验证器。
    • 这对我来说是新的,因为我是开发新手。 edit/add form 的那些代码将放在另一个结构文件中,或者放在我在content/digitafile.py 中编写的同一个文件中?
    猜你喜欢
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多