20

At the moment I have a custom validation attribute called ExistingFileName (below) but i have given it error messages to display

    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");
        }
    }

I have implemented it like so:

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

Im sure theres a way to define the error message when setting the attribute like below:

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

But I'm not sure how? Any help is greatly appreciated

4

2 回答 2

29

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; }

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

于 2013-07-04T16:34:47.727 回答
10

你继承自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属性的构造函数中设置的默认错误消息

希望有帮助。

于 2013-07-04T19:30:59.483 回答