2010-10-19 27 views
4

我剛剛掌握了自定義驗證屬性,並且我正在嘗試編寫一個自定義驗證attirbute,將放置在類級別以驗證我的模型的多個屬性。如何爲自定義驗證屬性中的不同場景設置多個錯誤消息?

我可以訪問我的模型的所有屬性,我希望能夠在我的IsValid超載檢查多個條件,並對其進行報告,具有不同的錯誤信息如下(簡單的例子)。

public override bool IsValid(object value) 
    { 
     var model = (MyObject) value; 

     //if this value is set, I don't want to do anything other checks 
     if (model.Prop3) 
     { 
      return true; 
     } 

     if (model.Prop1 == "blah" && model.Prop2 == 1) 
     { 
      ErrorMessage = "you can't enter blah if prop 2 equals 1"; 
      return false; 
     } 

     if(model.Prop1 == "blah blah" && model.Prop2 == 2) 
     { 
      ErrorMessage = "you can't enter blah blah if prop 2 equals 2"; 
      return false; 
     } 


     return true; 
    } 

但是當我這樣做,我得到第一時間的ErrorMessage異常引用「無法設置屬性不止一次。

現在我能拆了我的自定義屬性爲多個自定義屬性,但希望會有辦法做到這一點的一個,否則,我會重複在每個

//if this value is set, I don't want to do anything other checks 
     if (model.Prop3) 
     { 
      return true; 
     } 

我就已經搜索我的「一網打盡」,但找不到任何東西,所以道歉,如果我缺少任何明顯的東西。

在此先感謝!

回答

1

有趣的問題!我可以想到兩個解決方法。所以不是基於你想要的適當的解決方案,但他們可能有助於重新使用你的代碼。不能創建一個名爲MyCustomAttribute(或東西)一CustomAttribute抽象類,它覆蓋的IsValid下列方式:

public override bool IsValid(object value) 
{ 
    var model = (MyObject) value; 

    //if this value is set, I don't want to do anything other checks 
    if (model.Prop3) 
    { 
     return true; 
    } 

    CustomValidate(model); 
} 

CustomValidate(MyObject model)是你的抽象方法的話,你可以寫多個自定義屬性擴展MyCustomAttribute和純粹需要實現類特定場景的驗證邏輯。

所以,你可以有兩類:

public class BlahCustomAttribute : MyCustomAttribute 
{ 
    public override Boolean CustomValidate(MyObject obj) 
    { 
     if (model.Prop1 == "blah" && model.Prop2 == 1) 
     { 
      ErrorMessage = "you can't enter blah if prop 2 equals 1"; 
      return false; 
     } 
    } 
} 

public class BlahBlahCustomAttribute : MyCustomAttribute 
{ 
    public override Boolean CustomValidate(MyObject obj) 
    { 
     if (model.Prop1 == "blah" && model.Prop2 == 1) 
     { 
      ErrorMessage = "you can't enter blah blah if prop 2 equals 1"; 
      return false; 
     } 
    } 
} 

希望這有助於 - 你想不完全是,但會做的工作,其清潔爲好。

另一種解決方案是逗號分隔ErrorMessage屬性中的錯誤消息,並在前端處理它(但我會採用第一種方法)。

3

在MVC4可以覆蓋的IsValid作爲的ValidationResult

public class StrongPasswordAttribute : ValidationAttribute 
{ 
    protected override ValidationResult IsValid(object value, ValidationContext context) 
    { 
     if (value == null) 
      return new ValidationResult("Password is required"); 

     var val = value.ToString(); 

     if (!Regex.Match(val, @"^(?=.*[a-z]).{0,}$").Success) 
     { 
      return new ValidationResult("Password must contain at least one lower case letter"); 
     } 
     if (!Regex.Match(val, @"^(?=.*[A-Z]).{0,}$").Success) 
     { 
      return new ValidationResult("Password must contain at least one UPPER case letter"); 
     } 
     if (!Regex.Match(val, @"^(?=.*\d).{0,}$").Success) 
     { 
      return new ValidationResult("Password must contain at least one number"); 
     } 

     return ValidationResult.Success; 
    } 
} 
返回不同的消息
相關問題