2013-07-04 63 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