以编程方式创建敏捷类型时,是否可以验证传递给工厂的数据,就像它来自表单一样?
我有一个Dexterity内容类型,其中包含许多约束,不变量和&校验:
from zope import schema
from zope import interface
from z3c.form import validator
from plone import dexterity
from plone.directives import form
from five import grok
def is_not_foo(value):
return value is not 'foo'
class IMyType(form.Schema):
my_value = schema.TextLine(
constraint = is_not_foo
)
min = schema.Int(default=0)
max = schema.Int(default=99)
@interface.invariant
def max_more_than_min(data):
if data.min is not None and data.max is not None:
if data.min >= data.max:
raise interface.Invalid( u'Min is more than Max' )
@form.error_message(form=IMyType['my_value'], error=schema.interfaces.ConstraintNotSatisfied)
def error_my_value_is_foo(value):
return u'my_value must not be "foo"'
class ValidateMyValue(validator.SimpleFieldValidator):
def validate(self, value):
if value is not None:
# value passes some test
validator.WidgetValidatorDiscriminators(
ValidateMyValue,
form = IMyType['my_value'],
)
grok.global_adapter(ValidateMyValue)
为确保MyType的所有实例都具有正确的默认值,我使用FieldProperty为其创建了一个自定义类,并在类型的FTI声明中指定了它:
class MyType(dexterity.content.Item):
interface.implements(IMyType)
my_value = schema.fieldproperty.FieldProperty(IMyType['my_value'])
min = schema.fieldproperty.FieldProperty(IMyType['min'])
max = schema.fieldproperty.FieldProperty(IMyType['max'])
但是,当使用plone.dexterity.utils.createContentInContainer
时,可以实例化不符合所施加的约束等的对象。理想情况下,我希望能够在创建之前自己执行此验证。
这里有一个简单的方法吗?我已经简要介绍了collective.transmogrifier,但我没有看到输入验证。
感谢。
答案 0 :(得分:1)
您可以使用zope.schema.getValidationErrors()
function:
newInstance = createContentInContainer(folder, id, **schemavalues)
errors = getValidationErrors(IMyType, newInstance)
if errors:
# Schema not validated; errors is a sequence of (field, exception) tuples
# field is None if the error is for an invariant.