我正在尝试为 DateTime 编写自定义 ValidationAttribute?
但是,当它达到IsValid(object value)
该值时DateTime.Max
我在代码中看到的任何内容都没有设置此值,但这意味着我正在努力验证它。
我想要的规则是“Null or After Today”——这意味着如果有 Null 值,那很好,但如果有值,那么我想验证。
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; }
...