我有一個自定義驗證器類來驗證General Link
字段類型。它檢查Description
字段的字段值不超過15個字符。
但是,如果相同或另一個模板的鏈接字段需要20個字符。如何將參數傳遞給Sitecore中的自定義驗證器
有沒有辦法,我可以傳遞整數作爲參數。如果是這樣,如何通過&使用它。
此外,它可以在基本模板的字段級別完成。 (這樣我就可以定義每個這樣的字段限制)
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);
}
}
}
參數在'Rule'級別傳遞。如果我的項目中有6種類型的鏈接字段都有不同的限制,那麼我應該有6種不同的規則。我對嗎? – Qwerty
是的,用這個選項你會有規則「最大長度是20」,另一個「最大長度是10」 –
然後,在我的情況下,如果我必須爲單獨的案例創建規則,我不需要參數。對?我想知道我們是否可以在模板級傳遞參數。這樣只會有一個驗證器,但參數不同。希望你明白我的擔憂 – Qwerty