gpt4 book ai didi

asp.net - Sitecore CustomValidator 不会在 WFFM 自定义字段上触发

转载 作者:行者123 更新时间:2023-12-02 10:43:59 24 4
gpt4 key购买 nike

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

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 用户控制字段上运行良好。

最佳答案

您需要将验证代码移至实现 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/营销人员网络表单/设置/验证。将程序集和类添加到项目中。

现在,针对您的自定义字段,在“验证”字段中添加新的 BaseValidator 项,仅此而已。

请参阅 wffm custom form validator 的这篇文章完整示例

关于asp.net - Sitecore CustomValidator 不会在 WFFM 自定义字段上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com