[英]Using a Form Widget Validator on multiple fields
I've written a custom validator for my schema field as shown in the documentation here: http://docs.plone.org/develop/plone/forms/z3c.form.html#form-widget-validators我已经为我的架构字段编写了一个自定义验证器,如此处的文档所示: http : //docs.plone.org/develop/plone/forms/z3c.form.html#form-widget-validators
My question is that if i want to use the same validator for a few different fields, is that possible?我的问题是,如果我想对几个不同的字段使用相同的验证器,这可能吗? It doesnt seem to work.它似乎不起作用。 eg I would like to write:例如我想写:
# Set conditions for which fields the validator class applies
validator.WidgetValidatorDiscriminators(PhoneNumberValidator, field=IZohoContactForm['phone_number'])
validator.WidgetValidatorDiscriminators(PhoneNumberValidator, field=IZohoContactForm['another_phone_field'])
作为一种解决方法,我编写了两个具有不同名称的相同验证器,这违反了 DRY 原则,但我对此似乎无能为力……
It is possible to pass a field type as well for the field
argument (see: https://docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html#field-widget-validators ): validator.WidgetValidatorDiscriminators(MyListValidator, field=schema.List)
也可以为field
参数传递字段类型(请参阅: https : //docs.plone.org/develop/addons/schema-driven-forms/customising-form-behaviour/validation.html#field-widget -validators ): validator.WidgetValidatorDiscriminators(MyListValidator, field=schema.List)
In the above example the validator is applied to all fields of type schema.List
在上面的示例中,验证器应用于schema.List
类型的所有字段
It's an old question but I just recently faced this problem and this is my approach:这是一个老问题,但我最近才遇到这个问题,这是我的方法:
class ICaptchaSchema(model.Schema):
captcha = schema.TextLine(
title=_('security_check', default="Security check"),
)
class IFormSchema(model.Schema):
captcha1 = ICaptchaSchema['captcha']
captcha2 = ICaptchaSchema['captcha']
validator.WidgetValidatorDiscriminators(YourCustomValidator,
field=ICaptchaSchema['captcha'])
All fields "captcha" will be adapted.所有字段“验证码”都将进行调整。
Regards.问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.