locked
Validation not working correctly. RRS feed

  • Question

  • User-1251405508 posted

    Hello All,

    Not much action on the codeplex forum, so I decided to move my thread here.

    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
                Get
                    Return m_address
                End Get
            End Property
    
            Public ReadOnly Property Phone() As Phone
                Get
                    Return m_phone
                End Get
            End Property
    
            Private m_Name As String
            <NotNullValidator(MessageTemplate:="Name cant be empty")> _
            Public Property Name() As String
                Get
                    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
    


     

    Usage

            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)
            Next

    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.

    Thanks!

    Tuesday, March 30, 2010 2:33 PM

Answers

  • User-1251405508 posted

    It seems as though I forgot to add the ObjectValidator Attribute to the Address and Phone properties.

    Everything works great now.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2010 9:46 PM