Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Plone 在多个字段上使用表单小部件验证程序_Plone_Z3c.form - Fatal编程技术网

Plone 在多个字段上使用表单小部件验证程序

Plone 在多个字段上使用表单小部件验证程序,plone,z3c.form,Plone,Z3c.form,我已经为我的模式字段编写了一个自定义验证器,如以下文档所示: 我的问题是,如果我想对几个不同的字段使用相同的验证器,这可能吗?它似乎不起作用。我想写: # Set conditions for which fields the validator class applies validator.WidgetValidatorDiscriminators(PhoneNumberValidator, field=IZohoContactForm['phone_number']) validato

我已经为我的模式字段编写了一个自定义验证器,如以下文档所示:

我的问题是,如果我想对几个不同的字段使用相同的验证器,这可能吗?它似乎不起作用。我想写:

# 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原则,但对于这一个,我做不到多少,似乎

可以为
字段
参数传递字段类型 (请参阅:):
validator.widgetValidator鉴别器(MyListValidator,field=schema.List)


在上面的示例中,验证器应用于模式类型为
的所有字段。List

这是一个老问题,但我最近遇到了这个问题,这是我的方法:

  • 为要验证的字段设置架构接口
  • 使用表单架构界面中的字段:
  • 为第一个架构iterface注册表单小部件验证程序:
  • 所有字段“captcha”都将进行调整


    注意。

    在多个字段上使用相同的验证器应该可以正常工作。你有错误跟踪吗?我看到的行为是第二次注册覆盖了第一次注册。。。也就是说,如果我有字段A,B,然后为A注册验证器,然后为B注册验证器(如我的示例中所示),它只会为字段B激发。如果我交换注册顺序,那么注册B,然后为A,它只会为字段A激发。这就像wins中的最后一个。。。不,没有错误。您需要用验证器应用的字段列表调用它吗?表示“设置鉴别器”(复数)。我希望您可以向其传递一个列表或多个参数,但在z3c.form.validator.py:125中,它似乎只接受一个字段?def WidgetValidator鉴别器(验证器,上下文=无,请求=无,视图=无,字段=无,小部件=无):zope.component.adapter(util.getSpecification(上下文),util.getSpecification(请求),util.getSpecification(视图),util.getSpecification(字段),util.getSpecification(小部件))(验证器)作为一种解决方法,我已经编写了两个相同的验证器,它们的名称不同,这违反了DRY原则,但对于这一个,我所能做的不多。这个问题似乎有5年历史了,您可能不会得到响应。这可能更适合一个新问题。
    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'])