I'm trying to write a custom ValidationAttribute for a DateTime?
However, when it hits IsValid(object value)
the value is already DateTime.Max
Nothing I can see in my code has set this value, but it means I am struggling to validate it.
The rule I want is "Null or After Today" - which means if there is a Null value then it's fine, but if there is a value then I want to validate.
public class AfterTodayAttribute : ValidationAttribute, IClientValidatable
{
public AfterTodayAttribute()
{
}
//Tried this as well...same result
//protected override ValidationResult IsValid(object value, ValidationContext validationContext)
//{
// return base.IsValid(value, validationContext);
//}
public override bool IsValid(object value)
{
//value is already a DateTime and set to DateTime.Max
var valid = false; // for testing only
return valid;
}
}
...
[Display(Description = "Expiry Date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
[AfterToday(false, ErrorMessage = "The Limit Expiry Date must be later than Today")]
public DateTime? NewExpiryDate { get; set; }
...