plonebehaviordexterityz3c.form

access fields from a dexterity behavior on a content type invariant check


I have a behavior that defines two fields: year and week (of the year).

This behavior is reused for several content types, and only in one of them I need to make sure that this fields are not repeated in any other instance of the same content type, i.e. two objects of this content type can not share the same year and week (is fine to share the same year or the same week).

As this restriction is only meant for this specific content type I tried with an zope.interface.invariant but for some reason I can not get access to the fields defined in the behavior.

A simplified version of the Content type would be:

class IMyContentType(form.Schema)
    title = schema.TextLine(title="My title",
                            description="My description",
                            required=True,
    )

    @invariant
    def check_year_and_week(data):
        data.week

How can I get the value (if any) from within check_year_and_week invariant?


Solution

  • You can't. Invariants have access to values for other fields in the same interface, but not fields from other interfaces.

    You can use a widget manager validator instead: http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#widget-manager-validators

    Or do the validation in your form's action handler: http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers