开发者

How can you get the first validation error on a property only using Validation Application Block?

If I have two validators, a NotNul开发者_StackOverflow中文版lValidator and a StringLengthValidator, is there a way to get only a Null Validation Error and not both. For example:

public class Test
{
    [NotNullValidator(MessageTemplate="Name is required"),
    StringLengthValidator(1,50, MessageTemplate="Name must be between 1 and 50 characters")]
    public string Name { get; set; }
}

Test test = new Test {Name = null};
ValidationResults r = Validation.Validate(test);
if (!r.IsValid)
{
    foreach (var test in r)
    {
        Console.WriteLine(test.Message);
    }
}

In this case I get both validation errors. I get one telling me that the "Name is required" and another telling me that it should be between 1 and 50 characters. I only want to see that name is required in this case. Is this possible?


Just remove the NotNullValidatorAttribute and you're good to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜