ASP.NET MVC: DataAnnotation Authentication Procedure

I am having trouble understanding the validation logic that validates DataNotation data:

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 is displayed only if the required attribute is valid: I cannot receive both messages at the same time. Has anyone understood why? I think this is a problem with DefaultModelBinder, but still not found where and why.

+3
source share
1 answer

Class level validators are only performed if all validations at the property level have been successful. This behavior is encoded in the class ModelValidator.

+4

Source: https://habr.com/ru/post/1764464/


All Articles