I have a view model with 3 DateTime properties. StartDate EndDate ValueDate
All the date properties are optional except the EndDate. I have added a validation Attribute to the properties. My issue is when I call the IsValid(object value) method, the value parameter comes back with a default even if there is no data passed to the object.
How do I Validate a nullable DateTime object if Im expecting it's value to be null and the property is defaulting to a system default date.
public class TestClass
{
[CusomValidator]
public DateTime? StartDate {get;set;}
[CusomValidator]
public DateTime? EndDate {get;set;}
[CusomValidator]
public DateTime? ValueDate {get;set;}
}
public class CusomValidator: ValidationAttribute
{
public bool IsValida(object value)
{
//At this point value has the default system DateTime
// When I post the model, the properties which are suppose to be null are null and that is what I want
}
}
What I am trying to achieve he is: There is a minim mum date that is selectable, The minimum date is stored in the application settings (Properties.Settings). When ever the StartDate is set, then I dont want to apply the minimum date validation on the ValueDate, vice versa. The user interface allows on one of the two(StartDate and ValueDate) to be selected. If one is slecetd then the other is set to null and on the web (DataAnnotation) I only want to validate a DateTime? property that has a value.
DateTime?instead ofDateTime.) - Der Kommissar