Problem creating a custom validator

May 2, 2008 at 9:05 PM
Hi Guys

I'm having a hard time creation a custom validator using Enterprise library configuration tool.
When I attempt to set type of my Custom Validator, I keep getting the following error message:

There were no types found in the assembly 'XXXXXXXXXX' that implements or inherits from base type 'Microsoft.Practices.EnterpriseLibrary.Validation.Validator'.

wich is inaccurate since this is my code:

Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Imports Microsoft.Practices.EnterpriseLibrary.Validation


Namespace Entites

''' <summary>
''' Classe permettant de valider le champ TypeTransaction d'un objet GeneralKeaTransfertInformation
''' Cette validation est necessaire pour pouvoir effectuer le transfert des donnees
''' de l'objet GeneralKeaTransfertInformation vers KEA
''' </summary>
''' <remarks></remarks>
Public Class TypeTransactionValidator
Inherits Validator

Private _message As String
Private _raisonAvenant As String

Public Sub New(ByVal aMessage As String, ByVal uneRaisonAvenant As String)
MyBase.New(Nothing, Nothing)
_message = aMessage
_raisonAvenant = uneRaisonAvenant
End Sub


Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
Get
Return _message
End Get
End Property

Protected Overrides Sub DoValidate(ByVal objectToValidate As Object, ByVal currentTarget As Object, ByVal key As String, ByVal validationResults As Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults)
Dim typeTransaction As String = TryCast(objectToValidate, String)
If typeTransaction IsNot Nothing Then
If typeTransaction.Equals("Ave") And _raisonAvenant Is Nothing Then
Dim currentValidationResult As ValidationResult = New ValidationResult(_message, currentTarget, key, Nothing, TryCast(Me, validator))
validationResults.AddResult(currentValidationResult)
End If

End If
End Sub
End Class

End Namespace

Can somebody tell me what I'm doing wrong?

Thank you for any attention.

Amunike.

Amunike
May 5, 2008 at 4:32 PM
Hi,

Keep in mind that there's more than a name to a class identity. You need to make sure the version of EntLib you used to build the assembly where your validator resides matches the version of the configuration tool you're using. Eg if you built your assembly with a reference to the signed binaries, then you need to make sure you use a configuration tool compatible with the signed binaries like the stand alone tool started from the start menu, or the IDE integrated tool with the "ms signed" configuration set.

Fernando
May 6, 2008 at 4:37 PM
Hi fsimonazzi,

Thanks for your reply.
I've created a small test application and I've resolved the first issue. Now I have another error message: "MissingMethodExeption". The compiler cannot find the constructor for the type CustomValidationWindowsApplication.Entites.NameValidator.

here's my code:

This is the type I want to validate:

Imports CustomValidationWindowsApplication.Entites

Namespace Entites


Public Class Client

Public LastName As String
Public FirstName As String

Public Sub New(ByVal fName As String, ByVal lName As String)

Me.FirstName = fName
Me.LastName = lName

End Sub

End Class

End Namespace


This is my custom validator:

Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Configuration
Imports CustomValidationWindowsApplication.Entites

Namespace Entites


<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class NameValidator
Inherits Microsoft.Practices.EnterpriseLibrary.Validation.Validator(Of Client)

Public Sub New()

MyBase.New(Nothing, Nothing)
End Sub

Protected Overrides ReadOnly Property DefaultMessageTemplate() As String

Get
Return Me.MessageTemplate
End Get

End Property

Protected Overrides Sub DoValidate(ByVal objectToValidate As Client, ByVal currentTarget As Object, ByVal key As String, ByVal validationResults As Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResults)

If objectToValidate IsNot Nothing Then
If objectToValidate.FirstName.Equals("Jane") And objectToValidate.LastName Is Nothing Then
Dim currentValidationResult As ValidationResult = New ValidationResult(Me.MessageTemplate, currentTarget, key, Nothing, TryCast(Me, validator))
validationResults.AddResult(currentValidationResult)
End If

End If
End Sub
End Class

End Namespace

This is my client code:

Dim customValidator As Validator(Of Client) = ValidationFactory.CreateValidator(Of Client)() // The exception is raised here
Dim currentClient As New Client("Jane", Nothing)
Dim currentValidationResults As ValidationResults = customValidator.Validate(currentClient)
If Not currentValidationResults.IsValid Then
For Each aValidationResult As ValidationResult In currentValidationResults
Debug.WriteLine(aValidationResult.Message)
Next
End If

This is a portion of my config file:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<validation>
<type defaultRuleset="Rule Set" assemblyName="CustomValidationWindowsApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="CustomValidationWindowsApplication.Entites.Client">
<ruleset name="Rule Set">

<validator messageTemplate="Vous devez saisir un nom de famille lorsque le prenom du client est Jane" messageTemplateResourceName=""
messageTemplateResourceType="" tag="" type="CustomValidationWindowsApplication.Entites.NameValidator, CustomValidationWindowsApplication"
name="Custom Validator" />

<fields>
<field name="LastName" />
<field name="FirstName">
<validator negated="false" messageTemplate="Le prenom du client ne peut etre vide"
messageTemplateResourceName="" messageTemplateResourceType=""
tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Not Null Validator" />
</field>
</fields>
</ruleset>
</type>
</validation>


Thanks for any help guys.

Amunike
May 6, 2008 at 5:25 PM
Hi Amunike,

Custom validators, and custom providers in general, need to have a single-parameter constructor taking a NameValueCollection where the configuration information is received. This collection will contain the information from the attributes in the xml element corresponding to the validator entry (except those which are recognized as name and type).

Hope this helps,
Fernando
May 6, 2008 at 9:32 PM
Hi Fernando,

You've been very helpfull.....That solved my problem.

Thank you.

Amunike.