문제

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?

도움이 되었습니까?

해결책

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

다른 팁

A behavior is nothing but an adapter; if you're not getting the fields on the invariant you probably need adapt your content type before trying to access the extra fields.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top