none
DataAnnotation is not working. RRS feed

  • Question

  • Hey

    I try to implement DataAnnotations with my project. But no error is raised.

    I did the following steps to reproduce:

    Creating a new Console project and added the "System.ComponentModel.DataAnnotations" as reference

    Then I created a new folder called Data.
    Within this folder I added the following class:

    Namespace Data
        Public Class TestClass
            Public Property Id As Long
            Public Property Text As String
        End Class
    End Namespace

    And a second class:

    Imports System.ComponentModel.DataAnnotations
    
    Namespace Data
        <MetadataType(GetType(TestClass.Metadata))>
        Partial Public Class TestClass
            Partial Public Class Metadata
    
                <Required(ErrorMessage:="Is not allowed to be empty")>
                Public Text As Object
    
            End Class
    
        End Class
    End Namespace

    The next step was to add a new folder to the root of the project named "Validation"

    Here I added the code for the validation:

    Imports System.ComponentModel.DataAnnotations
    Namespace Validation
        Public Class EntityValidationResult
    
            Public Property Errors As IList(Of ValidationResult)
    
            Public ReadOnly Property HasError As Boolean
                Get
                    Return Errors.Count > 0
                End Get
            End Property
    
            Public Sub New(ByVal Optional ListOfErrors As IList(Of ValidationResult) = Nothing)
                Errors = If(ListOfErrors, New List(Of ValidationResult)())
            End Sub
        End Class
    
        Public Class EntityValidator(Of t)
    
            Public Function Validate(ByVal entity As t) As EntityValidationResult
                Dim locValidationResults = New List(Of ValidationResult)()
                Dim locValidationContext = New ValidationContext(entity, Nothing, Nothing)
                Dim locIsValid = Validator.TryValidateObject(entity, locValidationContext, locValidationResults, True)
    
                Return New EntityValidationResult(locValidationResults)
            End Function
        End Class
    
        Public Class ValidationHelper
    
            Public Shared Function ValidateEntity(Of T)(ByVal entity As T) As EntityValidationResult
    
                Return New EntityValidator(Of T)().Validate(entity)
            End Function
        End Class
    End Namespace
    


    Now I try to get the error:

        Sub Main()
            Dim locTestClass As New Data.TestClass
            Dim locResult = Validation.ValidationHelper.ValidateEntity(locTestClass)
    
            Console.WriteLine(locResult.HasError)
            Console.ReadLine()
        End Sub

    The problem is, that the error displays "False" but there must be the error, that Text is not allowed to be empty.

    Could someone please help me to get the correct result?

    THX in advice

    Sunday, March 11, 2018 12:32 PM

Answers

  • Some articles recommend this explicit registration of metadata:

    TypeDescriptor.AddProviderTransparent(New AssociatedMetadataTypeTypeDescriptionProvider(GetType(Data.TestClass), GetType(Data.TestClass.Metadata)), GetType(Data.TestClass))

     

    I think that it can be executed inside a shared constructor (‘Shared Sub New()’) of the TestClass.


    • Edited by Viorel_MVP Sunday, March 11, 2018 2:01 PM
    • Marked as answer by Zero-G. _ Sunday, March 11, 2018 2:03 PM
    Sunday, March 11, 2018 2:00 PM

All replies

  • Some articles recommend this explicit registration of metadata:

    TypeDescriptor.AddProviderTransparent(New AssociatedMetadataTypeTypeDescriptionProvider(GetType(Data.TestClass), GetType(Data.TestClass.Metadata)), GetType(Data.TestClass))

     

    I think that it can be executed inside a shared constructor (‘Shared Sub New()’) of the TestClass.


    • Edited by Viorel_MVP Sunday, March 11, 2018 2:01 PM
    • Marked as answer by Zero-G. _ Sunday, March 11, 2018 2:03 PM
    Sunday, March 11, 2018 2:00 PM
  • THX a lot - Yes, now it's working!

    Sunday, March 11, 2018 2:03 PM