views:

18

answers:

1

I'm having some trouble understanding validation logic behing DataAnnotation validation :

With the following model :

[AlwaysInvalid]
public class TestModel
{
    [Required]
    public string Test { get; set; }
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class AlwaysInvalidAttribute : ValidationAttribute
{
    private readonly object typeId = new object();

    public AlwaysInvalidAttribute() : base("Fail !") {}

    public override object TypeId { get { return this.typeId; } }

    public override bool IsValid(object value)
    {
        return false;
    }
}

The AlwaysInvalidAttribute error message gets displayed only if the Required attribute is valid : I can't get both messages at the same time. Anyone got an idea why ? I think it's an issue with DefaultModelBinder, but still haven't found where, or why.

+1  A: 

Class-level validators only run if all the property-level validators were successful. This behavior is coded up in the ModelValidator class.

marcind