在Sitecore 8.0中,我创建了一个新的WFFM验证来测试文件上传大小。我通过创建一个继承自FormCustomValidator的新类来创建匹配的Sitecore验证器,然后将新的验证器添加到自定义文件上载字段(Sitecore文件上载字段的副本)。不幸的是,在将新字段放在表单上时,验证并没有成功。 我错过了什么?
public class FileSizeValidator : FormCustomValidator
{
public int MaxFileSize
{
get
{
Log.Info("MaxFileSize get", this);
int maxSize;
if (int.TryParse(classAttributes["MaxFileSize"], out maxSize))
{
return maxSize;
}
return 0;
}
set
{
Log.Info("MaxFileSize set", this);
base.classAttributes["MaxFileSize"] = value.ToString();
}
}
public FileSizeValidator()
{
Log.Info("FileSizeValidator", this);
ServerValidate += new ServerValidateEventHandler(OnValidate);
}
private void OnValidate(object source, ServerValidateEventArgs args)
{
Log.Info("OnServerValidate", this);
FileUpload fileUpload = FindControl(ControlToValidate) as FileUpload;
if (!fileUpload.HasFile)
{
args.IsValid = true;
return;
}
args.IsValid = fileUpload.PostedFile.ContentLength <= MaxFileSize;
}
protected override bool EvaluateIsValid()
{
try
{
if (!string.IsNullOrEmpty(this.ControlToValidate))
return base.EvaluateIsValid();
else
return true;
}
catch (ArgumentOutOfRangeException ex)
{
return false;
}
}
protected override void OnLoad(EventArgs e)
{
this.ErrorMessage = "Error Message";
this.Text = "Text";
base.OnLoad(e);
}
}