Related to this question
I have created my own DateValidationAttibute to make sure a string is in a valid date format (e.g., MM/DD/YYYY)
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class DateValidationAttribute : DataTypeAttribute
{
public DateValidationAttribute() : base(DataType.Date){}
//MM/DD/YYYY, MM-DD-YYYY
public override bool IsValid(object value)
{
//validation logic
}
}
I am trying to test this attribute with this code
[Test]
public void Test()
{
var invalidObject = new TestValidation {DateField = "bah"};
var validationContext = new ValidationContext(invalidObject, null, null);
var validationResults = new System.Collections.Generic.List<ValidationResult>();
bool result = Validator.TryValidateObject(invalidObject, validationContext, validationResults);
Assert.IsFalse(result);
Assert.AreEqual(1, validationResults.Count);
}
private class TestValidation
{
[DateValidation(ErrorMessage = "Invalid Date!")]
public string DateField { get; set; }
}
Unfortunately this isn't working. I put a breakpoint at the DateValidationAttribute constructor, and the IsValid method. It definitely hits the constructor, but never hits the IsValid method. Any ideas?