2014-06-18 59 views
0

默認的StringLength驗證錯誤消息對我而言太長了,所以我想將它縮短。如何使用不顯眼的驗證在視圖模型中更改StringLength屬性的默認ErrorMessage屬性

我知道我可以指定每個屬性的ErrorMessage,但我不想重複這一切的地方:

[StringLength(25, ErrorMessage = "Max length is {1}")] 
[DisplayName("Address")] 
public virtual string BillAddress1 { get; set; } 

基於此帖子,Modify default ErrorMessage for StringLength validation,我子類StringLengthAttribute:

public class CustomStringLengthAttribute : StringLengthAttribute 
{ 
    public CustomStringLengthAttribute() 
     : this(20) 
    { 
    } 

    public CustomStringLengthAttribute(int maximumLength) 
     : base(maximumLength) 
    { 
     base.ErrorMessage = "Max length is {1}"; 
    } 
} 

並改爲使用CustomStringLengthAttribute代替:

[CustomStringLength(25)] 
[DisplayName("Address")] 
public virtual string BillAddress1 { get; set; } 

這個編譯良好,我在CustomStringLengthAttribute構造函數中設置了一個斷點來驗證它是否被命中,但不引人注意的驗證不再有效 - 將無效數據發送回控制器。

當我不使用我的子類版本時,它工作正常。

而且,這裏是JavaScript驗證:

if (validation.validateForm($('#addressForm'))) { 
    .... 
} 

validateForm:

function validateForm(form) { 
    prepareValidationScripts(form); 
    var validator = $.data(form[0], 'validator'); 
    return validator.form(); 
} 

function prepareValidationScripts(form) { 
    if (form.executed) 
     return; 
    form.removeData("validator"); 
    $.validator.unobtrusive.parse(document); 
    form.executed = true; 
} 

我缺少什麼?

謝謝。

回答

1

試試這個有自定義驗證。

CustomStringLengthAttribute.cs

public class CustomStringLengthAttribute : ValidationAttribute, IClientValidatable 
    { 
     public int MaximumLength 
     { 
      get; 
      private set; 
     } 
     public int MinimumLength 
     { 
      get; 
      set; 
     } 
     public CustomStringLengthAttribute(int maximumLength) 
      : base(new Func<string>(CustomStringLengthAttribute.GetDefaultErrorMessage)) 
     { 
      this.MaximumLength = maximumLength; 
     } 
     private static string GetDefaultErrorMessage() 
     { 
      return "Max length {1}"; 
     } 
     public override string FormatErrorMessage(string name) 
     { 
      return string.Format(base.ErrorMessageString, name, this.MaximumLength, this.MinimumLength); 
     } 
     public override bool IsValid(object value) 
     { 
      if (value == null) 
      { 
       return true; 
      } 
      string text = (string)value; 
      int maximumLength = this.MaximumLength; 
      int minimumLength = this.MinimumLength; 
      if (maximumLength < 0) 
      { 
       throw new InvalidOperationException("The maximum length must be a nonnegative integer."); 
      } 
      if (minimumLength > maximumLength) 
      { 
       throw new InvalidOperationException(string.Format("The maximum value '{0}' must be greater than or equal to the minimum value '{1}'.", maximumLength, minimumLength)); 
      } 
      int length = text.Length; 
      return length <= maximumLength && length >= minimumLength; 
     } 

     public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
     { 
      var rule = new ModelClientValidationRule(); 
      rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()); 
      rule.ValidationParameters.Add("max", MaximumLength); 
      rule.ValidationParameters.Add("min", MinimumLength); 
      rule.ValidationType = "customstringlength"; 
      yield return rule; 
     } 
    } 

在js文件:

/// <reference path="jquery-1.4.4.js" /> 
/// <reference path="jquery.validate.js" /> 
/// <reference path="jquery.validate.unobtrusive.js" /> 

if ($.validator && $.validator.unobtrusive) { 

    $.validator.unobtrusive.adapters.addMinMax("customstringlength", "min","max"); 

    $.validator.addMethod("customstringlength", function (value, element, min,max) { 
     if (value) { 
      if (value.length<min || value.length>max) { 
       return false; 
      } 
     } 
     return true; 
    }); 

} 

模型類:

[CustomStringLength(25)] 
[DisplayName("Address")] 
public virtual string BillAddress1 { get; set; } 
+0

謝謝你,我想你的建議,但它確實不太爲我工作 - 客戶端驗證現在已經開始,但是我得到了「Uncaught TypeError:無法讀取Jquery-1中未定義的屬性'調用'。 10.2.js(在Sizzle.matchesSelector中,行:「var ret = matches.call(elem,expr);」,雖然「matches」不是未定義的)。我不知道如何調試這個 - 有什麼建議嗎? –

+0

我將創建一個關於自定義驗證器問題的更多細節的新問題。 –

+0

此問題的解決方案位於以下位置:http://stackoverflow.com/questions/24298112/client-side-custom-annotation-validation-not-working/24313212#24313212 –

相關問題