2016-03-16 38 views
0

我有一個自定義驗證器類來驗證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); 
    } 
    } 
} 

回答

3

只看到驗證如何正則表達式是用來驗證郵件:

enter image description here

您添加參數,並驗證碼以後讀:

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); 
} 
+0

參數在'Rule'級別傳遞。如果我的項目中有6種類型的鏈接字段都有不同的限制,那麼我應該有6種不同的規則。我對嗎? – Qwerty

+0

是的,用這個選項你會有規則「最大長度是20」,另一個「最大長度是10」 –

+0

然後,在我的情況下,如果我必須爲單獨的案例創建規則,我不需要參數。對?我想知道我們是否可以在模板級傳遞參數。這樣只會有一個驗證器,但參數不同。希望你明白我的擔憂 – Qwerty

2

普萊舍檢查下一類:由模板 verifing字段的最大長度這條規則被分配到一個字段。

[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); 
    } 

} 
+0

您能否告訴我參數在內容樹中的定義? – Qwerty

+0

這將需要我爲不同的鏈接字段創建多個Validator類。我想只有一個Validator類,接受參數。這可能嗎。 – Qwerty

+0

抱歉被分配到一個字段而不是一個模板 –

相關問題