'Which IsValid method should I override when implementing custom ValidationAttribute

When I implement a custom attribute inherited from ValidationAttribute class, I've always overrided bool IsValid(object value) method disgarding the other one whose prototype is ValidationResult IsValid(objet value, ValidationContext validationContext).

Maybe, I should override the second method instead even if I don't use validation context or the result (I use validation with EntityFramework and ModelState.IsValid controller property). Or continue to disregard the overloaded method. If then, could I have an object valid or invalid depending on the context the attribute validation is invoked? Is a situation as shown in the code bellow problematic?

class StrictlyPreviousAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var dateTime = value as DateTime?;
        return dateTime == null || dateTime <= DateTime.Today;
    }
}

class PreviousAttribute : StrictlyPreviousAttribute 
{
    public override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var dateTime = value as DateTime?;

        if(dateTime == DateTime.Today)
        {
            return ValidationResult.Success;
        }
        else
        {
            return base.IsValid(object);
        }
    }
}

I don't know if there is there object misconception or if I'm missing some point. Is there a method I should preferably override instead of the other. Should I override both.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source