我有一个自定义验证程序类来验证General Link
字段类型。它检查Description
字段的字段值是否不超过15个字符
但是,如果相同或另一个模板的链接字段需要20个字符,该怎么办?
有没有办法,我可以将整数作为参数传递。如果是这样,如何通过&用它。
此外,它可以在基本模板的字段级别完成。 (这样我就可以为每个这样的字段定义限制)
namespace CustomValidators
{
// This validator ensures that the description attribute of a link is either empty or has length of 15 characters.
[Serializable]
public class LinkTextValidator : StandardValidator
{
public override string Name {get { return "Link text validator"; } }
public LinkTextValidator() { }
public LinkTextValidator(SerializationInfo info, StreamingContext context) : base(info, context) { }
protected override ValidatorResult Evaluate()
{
Field field = this.GetField();
if (field == null)
return ValidatorResult.Valid;
string fieldValue = this.ControlValidationValue;
if (string.IsNullOrEmpty(fieldValue) || string.Compare(fieldValue, "<link>", StringComparison.InvariantCulture) == 0)
return ValidatorResult.Valid;
XmlValue xmlValue = new XmlValue(fieldValue, "link");
string attribute = xmlValue.GetAttribute("text");
if (!string.IsNullOrEmpty(xmlValue.GetAttribute("text")) && Convert.ToString(xmlValue.GetAttribute("text")).Length > 15)
{
this.Text = this.GetText("Description field should have not more than 15 characters, in the link field \"{0}\".", field.DisplayName);
return this.GetFailedResult(ValidatorResult.FatalError);
}
else
{
return ValidatorResult.Valid;
}
}
protected override ValidatorResult GetMaxValidatorResult()
{
return this.GetFailedResult(ValidatorResult.FatalError);
}
}
}
答案 0 :(得分:3)
了解如何使用正则表达式验证程序验证电子邮件:
您可以添加参数并稍后在Validator代码中读取它们:
protected override ValidatorResult Evaluate()
{
string controlValidationValue = this.ControlValidationValue;
if (string.IsNullOrEmpty(controlValidationValue))
return ValidatorResult.Valid;
string pattern = this.Parameters["Pattern"];
if (string.IsNullOrEmpty(pattern) || new Regex(pattern, RegexOptions.IgnoreCase).IsMatch(controlValidationValue))
return ValidatorResult.Valid;
this.Text = this.GetText("The field \"{0}\" does not match the regular expression \"{1}\".", this.GetFieldDisplayName(), pattern);
return this.GetFailedResult(ValidatorResult.Error);
}
答案 1 :(得分:2)
请检查下一课:按模板验证字段的最大长度 此规则已分配给字段。
[Serializable]
public class MaxLengthFieldbyTemplateValidator : StandardValidator
{
/// <summary>
/// The template separator in Parameters field
/// </summary>
private const char FieldLengthSeparator = '=';
/// <summary>
/// The template length separator in Parameters field
/// </summary>
private const char TemplateLengthSeparator = '~';
/// <summary>
/// Gets the name.
///
/// </summary>
///
/// <value>
/// The validator name.
/// </value>
public override string Name
{
get
{
return "Max Length";
}
}
/// <summary>
/// Initializes a new instance of the <see cref="T:Sitecore.Data.Validators.FieldValidators.MaxLengthFieldValidator"/> class.
///
/// </summary>
public MaxLengthFieldbyTemplateValidator()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="T:Sitecore.Data.Validators.FieldValidators.MaxLengthFieldValidator"/> class.
///
/// </summary>
/// <param name="info">The serialization info.
/// </param><param name="context">The context.
/// </param>
public MaxLengthFieldbyTemplateValidator(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
/// <summary>
/// When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.
///
/// </summary>
///
/// <returns>
/// The result of the evaluation.
///
/// </returns>
protected override ValidatorResult Evaluate()
{
return IsValid(GetItem().TemplateID.ToString()) ? ValidatorResult.Valid : this.GetFailedResult(ValidatorResult.CriticalError);
}
private bool IsValid(string currentItemTemplateId)
{
var validatorParameters = Parameters;
// parsing all validators,they are splited by & char
foreach (var currentParam in validatorParameters)
{
//checking if current item template id is in parameters value
if (currentParam.Value.Contains(currentItemTemplateId))
{
if (currentParam.Value.Contains(TemplateLengthSeparator))
{
var maxLenghKeyValuePair = currentParam.Value.Split(TemplateLengthSeparator)[1];
if (maxLenghKeyValuePair.Contains(FieldLengthSeparator))
{
var maxLengthValue = maxLenghKeyValuePair.Split(FieldLengthSeparator)[1];
int intMaxLengthValue = MainUtil.GetInt(maxLengthValue, 0);
string controlValidationValue = this.ControlValidationValue;
if (string.IsNullOrEmpty(controlValidationValue) ||
controlValidationValue.Length <= intMaxLengthValue)
{ return true; }
else
{
Text = GetText("The maximum length of the field \"{0}\" is {1} characters.", this.GetFieldDisplayName(), maxLengthValue);
return false;
}
}
}
}
}
return true;
}
/// <summary>
/// Gets the max validator result.
///
/// </summary>
///
/// <remarks>
/// This is used when saving and the validator uses a thread. If the Max Validator Result
/// is Error or below, the validator does not have to be evaluated before saving.
/// If the Max Validator Result is CriticalError or FatalError, the validator must have
/// been evaluated before saving.
///
/// </remarks>
///
/// <returns>
/// The max validator result.
///
/// </returns>
protected override ValidatorResult GetMaxValidatorResult()
{
return this.GetFailedResult(ValidatorResult.CriticalError);
}
}