0

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 DataAnnotations, 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?

Kyle W
  • 3,702
  • 20
  • 32
  • Use the second overload of [`Validator.TryValidateProperty`](http://msdn.microsoft.com/en-us/library/dd411772.aspx) and specify `validateAllProperties = true`. Otherwise the validator will only validate the `Required` attributes. – Styxxy Jun 23 '15 at 21:39
  • My brain must be tired, I was linking to TryValidateObject. Sorry for the mistake. On a sidenote, why not use the TryValidateObject instead of writing the exact same thing in above code? – Styxxy Jun 23 '15 at 21:49
  • @Styxxy Your guess is as good as mine, I didn't write it :) I went ahead and tried it, and it exhibited the same behavior. – Kyle W Jun 23 '15 at 21:54
  • Kyle, it seems like the library is incomplete. The `IsValid` function throws a `NotImplementedException`. The library is also dead. So best thing is to fork it and do some adjustments. Example adjustments: http://pastebin.com/thQERyy2 – Styxxy Jun 23 '15 at 22:29
  • @Styxxy I see that it's dead, I'm not seeing that it's incomplete. The `LessThanOrEqualTo` class only has a constructor to call `Is`, which has a valid implementation of `IsValid` – Kyle W Jun 24 '15 at 14:51
  • The IsValid method you are referring to, is the wrong overload. – Styxxy Jun 24 '15 at 18:15

0 Answers0