如何自定义验证属性错误信息?

Srb*_*711 13 c# validation asp.net-mvc-4

目前我有一个名为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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

我已经像这样实现了它:

[ExistingFileName]
public string NameOfImage { get; set; }
Run Code Online (Sandbox Code Playgroud)

我确定这是一种在设置属性时定义错误消息的方法,如下所示:

[ExistingFileName(errormessage="Blah blah blah")]
public string NameOfImage { get; set; }
Run Code Online (Sandbox Code Playgroud)

但我不知道怎么样?任何帮助是极大的赞赏

Sim*_*ger 20

不要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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的注释中:

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

如果您没有明确设置自定义消息,它将回退到自定义属性中的预定义常量.


Ami*_*ila 5

你是继承人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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以使用它来验证您的字段/属性

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

以及是否按以下方式使用它。

[ExistingFileName]
public string NameOfImage { get; set; }
Run Code Online (Sandbox Code Playgroud)

然后,它将使用在ExistingFileName属性的构造函数中设置的默认错误消息

希望能有所帮助。

  • 覆盖方法是“protected override ValidationResult IsValid(object value, ValidationContext validContext)”。返回 `ValidationResult.Success` 而不是 `true`,并返回 `new ValidationResult("My custom error message")` 而不是 `false`。 (2认同)