Problem with Validation.

Topics: Validation Application Block
Mar 30, 2010 at 7:10 PM

Hello All,

I have a simple scenario that I need some help with.

I have a class file called Address which stores address, city, state, zip, ect, and a class file called Phone which stores the number, phonetype, ect..

Then I have a Business Object called Contact, with a PROPERTY called Address which references a new instance of the address class, and a PROPERTY called Phone which references a new instance of the phone class.

The Address class has its own Validation Attributes decorating the properties, same with the phone class

When I try to Validate the Contact, it does not validate the properties of the address class or the phone class.

How can achieve this functionality?

Below is a sample reproducing the issue.

    Public Class Address
        <NotNullValidator(MessageTemplate:="Line 1 can not be empty")> _
        Public Line1 As String
        Public Line2 As String
        <NotNullValidator(MessageTemplate:="City can not be empty")> _
        Public City As String
    End Class

    Public Class Phone
        <NotNullValidator(MessageTemplate:="Number can not be empty")> _
        Public Number As String
        Public Type As String
    End Class

    Public Class Contact : Inherits BaseDomainObect
        Private m_address As Address = New Address
        Private m_phone As Phone = New Phone

        Public ReadOnly Property Address() As Address
                Return m_address
            End Get
        End Property

        Public ReadOnly Property Phone() As Phone
                Return m_phone
            End Get
        End Property

        Private m_Name As String
        <NotNullValidator(MessageTemplate:="Name cant be empty")> _
        Public Property Name() As String
                Return m_Name
            End Get
            Set(ByVal value As String)
                m_Name = value
            End Set
        End Property

    End Class

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

    End Class

Test validation

        Dim myContact As New Contact
        myContact.Name = Nothing

        Dim results As ValidationResults
        results = Validation.Validate(myContact)
        For Each r As ValidationResult In results
            MessageBox.Show(r.Key & "   " & r.Message)

The only property that gets validated is the Name property.

How can I drill down and validate the properties within the address and phone classes?

Any help would be greatly appreciated.


Mar 31, 2010 at 12:06 AM

Hi intertekdev

To validate the Address and Phone objects, you must decorate the Address and Phone properties of the Contact class with the ObjectValidator attribute.

Public ReadOnly Property Address() As Address
End Property

As per the Enterprise Library documentation, the ObjectValidator attribute "causes validation to occur on an object reference. All validators defined for the object's type will be invoked, just as if the Validation.Validate method had been called on the object."



Mar 31, 2010 at 2:41 AM

Thank you Chris,

That worked beautifully.

I would have never found that.

Thanks again, I really appreciate your help.