pythonschemaplonedexterityplone-4.x

How I Fix in Dexterity Plone 4 error in Schema?


I'm writing this code for Dexterity in Plone 4, the code is simple, however, it causes a certain error, below we see the code and then an image of the error, there I explain what I did.

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

When I will create in Plone the object called DigitalFile, I start to fill in the fields, when all fields are filled, I have this error before clicking submit:

Module projetime.ged.content.digitalfile, line 87, in DocumentTypeInvariantModule; z3c.form.validator, line 164, in __getattr__;AttributeError: Description

How I fix it?


Solution

  • The invariant is defined in the schema and the Description Field is not in this schema, thus you cannot access it in the invariant.

    You need something that take in account, that the Description field is defined in a different schema.

    You can solve your problem with a Form validator: http://docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers

    For this you need to register a custom edit/add form: Check:http://docs.plone.org/external/plone.app.dexterity/docs/advanced/custom-add-and-edit-forms.html

    This is all very well documented, for plone 4 and plone 5.