I have added a reference to MVC Foolproof Validation
to my project. I am using one of the annotations on my model:
<Required(ErrorMessage:="Start Date is required.")>
<LessThanOrEqualTo("EndDate", ErrorMessage:="Start Date must not be after End Date.")>
Public Property StartDate As DateTime
Get
Return Item.StartDate
End Get
Set(value As DateTime)
Item.StartDate = value
End Set
End Property
<Required(ErrorMessage:="End Date is required.")>
Public Property EndDate As DateTime
Get
Return Item.EndDate
End Get
Set(value As DateTime)
Item.EndDate = value
End Set
End Property
We have a validation class which validates like so:
Dim results As New List(Of ValidationResult)()
Debug.Assert(model IsNot Nothing)
Dim modelProperties = New List(Of PropertyInfo)
GetModelPropertiesRecursively(model.GetType(), modelProperties)
For Each pi As PropertyInfo In modelProperties
Dim result = Validator.TryValidateProperty(pi.GetValue(model),
New ComponentModel.DataAnnotations.ValidationContext(model, Nothing, Nothing) With {.MemberName = pi.Name}, results)
Next
Dim modelName = model.GetType().Name
Dim mapped = results.Select(Function(result)
Debug.Assert(result.MemberNames.Any())
Return New ValidationError(modelName, result.MemberNames.First(), result.ErrorMessage)
End Function)
For Each validationError As ValidationError In mapped
context.ValidationErrors.Add(validationError)
Next
This works for all the regular DataAnnotation
s, but it's not catching it for the new annotation. I can't debug into Validator.TryValidateProperty
, but the result comes back as true for the property, despite being after the EndDate
.
Is the Validation logic flawed?