Problem Validating Object with E.L.

Topics: Validation Application Block
Mar 29, 2010 at 6:47 PM

Hello All,

Im having some issues with my validation.

I created a base class from which all of my business objects will inherit from.

I added a method called Validate and attempted to validate the class, but it always returns IsValid = True even though I purposely violated several rules.

Here is a small example of the scenario.

Base Domain Object

Public MustInherit Class BaseDomainObect
        Public Function Validate() As ValidationResults
            Return Validation.Validate(Me)
        End Function
End Class

Test Business Object

    Public Class TestObject : Inherits BaseDomainObect

        <NotNullValidator()> _
        Public Property TestProperty() As String
                Return Nothing
            End Get
            Set(ByVal value As String)
                'do nothing
            End Set
        End Property
    End Class

and here is how I was trying to use it.

 Dim results, results2 As ValidationResults

        Dim testo As New TestObject
        results = Validation.Validate(testo) 'returns the correct result
        results2 = testo.Validate 'always returns true

        For Each r As ValidationResult In results
            MessageBox.Show(r.Key & "   " & r.Message)

When I call the Validation method of the business object it always returns true even when a rule has clearly been violated.

Any ideas what I did wrong?


Mar 30, 2010 at 3:16 AM

Hi intertekdev,

Please see this FAQ item, there is a workaround suggested, please see if it is feasible for you .

Also, see if this may help




Mar 30, 2010 at 3:33 AM

Thanks but the links you pointed me to do not help.

I think you misunderstood my problem.

The links you provided talk about people having an issue when they inherit from a base class that contains properties that are decorated with attributes.

This is clearly not my problem.

My base class only provides a validation method as shown above in the example.

The problem is the Validate method in the base class does not work correctly. The IsValid property of ValidationResult that is returned is always True, even when I purposely violated a rule, as shown again in my example above.

Clearly the issue lies in the base class, and I believe it has to do with the Validation.Validate(Me) line.

As shown in the example above when I validate the object directly I get the correct result, but I try the validation method within the business object it fails.


Mar 30, 2010 at 4:38 AM

try using this as your Validate Method in your Base class

Public Function Validate(ByVal type As Type) As ValidationResults
    Dim val As Validator = ValidationFactory.CreateValidator(type)
    Dim results As New ValidationResults()
    val.Validate(Me, results)
    Return results
End Function

then executing the Validate method in your client code by

     Dim testo As New TestObject

      Dim resultsFromBase As ValidationResults = testo.Validate(GetType(TestObject))

Hope this helps.




Mar 30, 2010 at 6:24 PM


I modified your code so I dont have to pass in the type.

    Public MustInherit Class BaseDomainObect
        Public Function Validate() As ValidationResults
            Return Validation.Validate(Me.GetType)

        End Function
    End Class

And this works great.