2013-07-04 65 views
8

目前,我有一個名爲ExistingFileName (下同)自定義驗證屬性,但我已經給它的錯誤消息顯示如何自定義驗證屬性錯誤信息?

protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext) 
    { 
     if (value!=null) 
     { 
      string fileName = value.ToString(); 
      if (FileExists(fileName)) 
      { 
       return new ValidationResult("Sorry but there is already an image with this name please rename your image"); 
      } 
      else 
      { 
       return ValidationResult.Success; 
      } 
     } 
     else 
     { 
      return new ValidationResult("Please enter a name for your image"); 
     } 
    } 

我已經實現了它,像這樣:

[ExistingFileName] 
public string NameOfImage { get; set; } 

我確信在設置如下屬性時可以定義錯誤信息:

[ExistingFileName(errormessage="Blah blah blah")] 
public string NameOfImage { get; set; } 

但我不知道如何?非常感謝任何幫助

回答

13

而不是使用預定義的字符串返回ValidationResult,請嘗試使用ErrorMessage屬性或任何其他自定義屬性。例如:

private const string DefaultFileNotFoundMessage = 
    "Sorry but there is already an image with this name please rename your image"; 

private const string DefaultErrorMessage = 
    "Please enter a name for your image"; 

public string FileNotFoundMessage { get; set; } 

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{ 
    if (value!=null) 
    { 
     string fileName = value.ToString(); 
     if (FileExists(fileName)) 
     { 
      return new ValidationResult(FileNotFoundMessage ?? 
             DefaultFileNotFoundMessage); 
     } 
     else 
     { 
      return ValidationResult.Success; 
     } 
    } 
    else 
    { 
     return new ValidationResult(ErrorMessage ?? 
            DefaultErrorMessage); 
    } 
} 

而在你的註釋:

[ExistingFileName(FileNotFoundMessage = "Uh oh! Not Found!")] 
public string NameOfImage { get; set; } 

如果不明確地設置自定義的消息,它將退回到您的自定義屬性的預定義的常量。

3

您是從ValidationAttribute繼承的嗎?

那麼你不需要把它保存在一個單獨的變量。當您從類別ValidationAttribute繼承時,所有錯誤消息代碼都可用。現在

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
public class ExistingFileNameAttribute : ValidationAttribute 
{ 
    public string FileFoundMessage = "Sorry but there is already an image with this name please rename your image"; 
    public ExistingFileNameAttribute() 
     : base("Please enter a name for your image") 
    {    
    } 

    public override ValidationResult IsValid(object value) 
    { 
     if (value!=null) 
     { 
      string fileName = value.ToString(); 
      if (FileExists(fileName)) 
      { 
       return new ValidationResult(FileFoundMessage); 
      } 
      else 
      { 
       return ValidationResult.Success; 
      } 
     } 
     else 
     { 
      return new ValidationResult(ErrorMessage); 
     } 
    } 
} 

,你可以用它來驗證您的域/屬性

[ExistingFileName(ErrorMessage="Blah blah blah", FileFoundMessage = "Blah Bla")] 
public string NameOfImage { get; set; } 

,如果你使用它像下面。

[ExistingFileName] 
public string NameOfImage { get; set; } 

那麼,它將使用在ExistingFileName的構造函數中設置默認的錯誤消息屬性

希望有所幫助。

+0

很多,謝謝你。 –

+0

你的'IsValid'應該返回一個布爾值,但是你返回一個'ValidationResult'。它是否正確?我無法讓它工作,我無法重寫'IsValid'來返回'ValidationResult'。 – muttley91

+0

感謝您指出,返回類型必須是ValidationResult – Amila