Sitecore CustomValidator不会在WFFM自定义字段上触发 [英] Sitecore CustomValidator does not fire on WFFM custom field

查看:86
本文介绍了Sitecore CustomValidator不会在WFFM自定义字段上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Sitecore上的WFFM上扩展SingleLineText字段.该字段将具有CustomValidator.但是,当页面回发时,ServerValidate事件不会触发.下面的代码片段.

I've try to extend SingleLineText field on WFFM on Sitecore. This field will have CustomValidator. But ServerValidate event does not fire when page postbacked. The snipped code below.

public class SingleLineText : Sitecore.Form.Web.UI.Controls.SingleLineText
{
   protected override void OnInit(EventArgs e)
   {
       base.OnInit(e);

       var validator = new CustomValidator() { Display = ValidatorDisplay.None };
       validator.ServerValidate += this.Validator_ServerValidate;
       this.generalPanel.Controls.Add(validator);
    }
    protected void Validator_ServerValidate(object source, ServerValidateEventArgs args)
    {
         // does not fire
         var validator = source as IValidator;
         args.IsValid = this.IsValid(validator);
    }
}

相同的代码在具有ascx的ustom用户控制字段上可以正常工作.

The same code works fine on ustom user control field which has ascx.

推荐答案

您将需要将验证代码移至实现FormCustomValidator的新类.

You will want to move your validation code to a new class that implements FormCustomValidator.

public class MySingleLineTextValidator : FormCustomValidator
{ 
    protected override bool EvaluateIsValid()
    {
        if (!String.IsNullOrEmpty(base.ControlToValidate))
        {
            Control controlToValidate = this.FindControl(base.ControlToValidate);
            //Code to validate
        }

        return false;
    }}

然后,您将需要在WFFM验证文件夹中添加一个BaseValidator项,通常是此路径; /sitecore/system/Modules/Web Forms for Marketers/Settings/Validation.将程序集和类添加到该项目.

Then you will need add a BaseValidator Item in the WFFM Validation Folder, usually this path; /sitecore/system/Modules/Web Forms for Marketers/Settings/Validation. Add the assembly and class to the item.

现在针对您的自定义字段,在Validation字段中添加新的BaseValidator项.

Now against your custom Field, add the your new BaseValidator item in the Validation field and that's it.

请参见 wffm自定义表单验证器以获取完整示例

这篇关于Sitecore CustomValidator不会在WFFM自定义字段上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆