从内容类型不变检查的灵巧行为中访问字段

时间:2013-11-14 09:58:32

标签: plone behavior dexterity z3c.form

我的行为定义了两个字段:yearweek(年份)。

此行为会重复用于多种内容类型,并且只在其中一种内容类型中我需要确保此字段不会在相同内容类型的任何其他实例中重复,即此内容类型的两个对象无法共享相同的year week(可以在同一周分享同一年)。

由于此限制仅针对此特定内容类型,我尝试使用zope.interface.invariant,但由于某种原因,我无法访问行为中定义的字段。

内容类型的简化版本为:

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

    @invariant
    def check_year_and_week(data):
        data.week

如何从check_year_and_week不变量

中获取值(如果有)?

2 个答案:

答案 0 :(得分:4)

你不能。不变量可以访问同一接口中其他字段的值,但不能访问其他接口的字段。

您可以使用小部件管理器验证器:http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#widget-manager-validators

或者在表单的操作处理程序中进行验证:http://developer.plone.org/reference_manuals/active/schema-driven-forms/customising-form-behaviour/validation.html#validating-in-action-handlers

答案 1 :(得分:0)

行为只不过是一个适配器;如果你没有获得不变量的字段,你可能需要在尝试访问额外的字段之前调整你的内容类型。