Usage of Validation Application block

Topics: Validation Application Block
Nov 25, 2010 at 9:35 AM

Hi,

I am new to entlib and I want to encapsulate the validation functionality inside a user defined function

with parameters specified and return true or false. can any one send me the step by step procedure for it.

Thanks in advance

dodda

Nov 25, 2010 at 9:53 AM

This is the basic code for validation:

ValidationResults results = Validation.Validate(objectInstance);

The indication if there are any validation errors is in the IsValid property of the ValidationResults object.   So basically, that's just what you need to put in your function, add parameter for the actual object to validate and return results.IsValid.

Note that in version lower than 5.0, the ValidationValidate() method validates the passed object based on the type which it is declared as, thus if your method uses the type Object as the parameter type, the validation ruls that it will look for is the one which is configured of type Object.  However, this was changed in version 5.0.  It will be validated using validation rules based on its actual type.  Thus, if the actual type is of type Customer, it will be validated based on rule set defined for Customer type even if it was passed as of type Object.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 25, 2010 at 10:02 AM

Hi,

Thanks for the reply and right now I write the code like this

<StringLengthValidator(1, 50, MessageTemplate:="Name must be between 1 and 50 characters")> _
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

my question is, how can i write the code as a function with a scope to pass the parameters

Nov 25, 2010 at 10:04 AM

I'm sorry but I'm not clear on what is it that you're trying to accomplish.  What do you mean "with a scope to pass the parameters"?  What are those parameters? 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 25, 2010 at 10:09 AM

Just like the following function,

Public Function bName(ByVal sName As String, ByVal length As Integer, ByRef sOutputString As String) As Boolean
        Dim strName As String
        Dim intLength As Integer
        bName = False

        Try
            strName = sName
            intLength = length
            //I need to write the stringlength validator functionality here with length as a parameter
        Catch ex As Exception
            sOutputString = ex.ToString
        End Try
    End Function

Nov 25, 2010 at 10:17 AM

You mean manually create a StringLengthValidator?

StringLengthValidator stringLengthValidator = new StringLengthValidator(intLength)
stringLengthValidator.Validate(strName);

There are other overloads when creating StringLengthValidator so check which one you need.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 25, 2010 at 10:21 AM

yes, need to create manually.

Thanks for the reply and I will try some..

Nov 26, 2010 at 7:39 AM

Hi Sarah,

I sent an email, please check and let me know.

waiting for your reply

Nov 26, 2010 at 8:05 AM

Based on  your email, do you really need to write separate functions for each?  Because EntLib actually has built in features for validating textbox input.  I'm talking about the PropertyProxyValidator for ASP.NET integration and the ValidationProvider for Winforms integration.  You can check them out in the quickstart which comes with 4.1.

Just want to make sure you know your easier option.  If this option isn't what you need, let me know.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 8:13 AM
Edited Nov 29, 2010 at 1:48 AM

Thanks for ur reply and

My requirement is like that, I need to write separate function for each field validation like following

 

Public Function Name(ByVal sName As String, ByVal length As Integer, ByRef sOutputString As String) As Boolean

//validation goes here and returns true or false

End Function

 

 

 

Nov 26, 2010 at 8:24 AM

Then your approach would be to create the validators you need within each method, just like the sample I posted on my second reply.

StringLengthValidator stringLengthValidator = new StringLengthValidator(intLength)
return stringLengthValidator.Validate(strName).IsValid;

Just create the validator you need, call the Validate method against it, and return the IsValid property of the resulting ValidationResults object. 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 8:32 AM
Edited Nov 26, 2010 at 8:45 AM

HI Sarah,

 

Thanks for ur reply

Nov 26, 2010 at 8:39 AM
Edited Nov 26, 2010 at 8:40 AM

If you want to validate that an integer is non negative, then create a RangeValidator, specify a value of 0 in the LowerBound property and set the UpperBoundType property to RangeBoundaryType.Ignore. 

I think what you need, and I strongly recommend to check out each of Validation Application Block's built-in validators.  They are the most commonly used validators so check out their functionalities.  I think you can pretty much find answers to your questions by going through each and their properties.  Anyway, their names are also informational and readily tells what they're all about. 

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 8:58 AM

Ok. I got some idea on how can we customize the built in functionality of entlib.

Thank you very much for ur support and I will post you If I need any clarification in future.


From: AvanadeSupport [mailto:notifications@codeplex.com]
Sent: Friday, November 26, 2010 4:40 PM
To: Hariprasad Dodda
Subject: Re: Usage of Validation Application block [entlib:235989]

From: AvanadeSupport

If you want to validate that an integer is non negative, then create a RangeValidator, specify a value of 0 in the LowerBound property and set the UpperBoundType property to RangeBoundaryType.Ignore.

I strongly recommend checking out each of Validation Application Block's built-in validators. They are the most commonly used validators so check out their functionalities. I think you can pretty much find answers to your questions by going through each and their properties. Anyway, their names are also informational and readily tells what they're all about.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Read the full discussion online.

To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com@discussions.codeplex.com)

To start a new discussion for this project, email entlib@discussions.codeplex.com@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

Dec 31, 2010 at 4:33 AM

Hi Sarah,

I have created all my validator functions in one class library and I am trying to use this class library dll

in an executable project (web application), but I am getting the error saying that

Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

the code that used to validate is like this

Dim Validation As New WebInputValidator()
        Validation.Email = txtEmail.Text
        Dim validator As Validator(Of WebInputValidator) = ValidationFactory.CreateValidator(Of WebInputValidator)()
        Dim results As ValidationResults = validator.Validate(Validation)

        If Not results.IsValid Then
            For Each result In results
                lblErrorMsg.Text = result.Message
            Next
        Else
            lblErrorMsg.Text = "Input Supplied"
        End If

 please suggest me how can I use the dll.

Jan 2, 2011 at 11:13 PM
Edited Jan 2, 2011 at 11:37 PM

This is actually listed in the Entlib FAQ.   If you edited your configuration file using the configuration tool integrated with Visual Studio, make sure you referenced the entlib assemblies from the installation folder (Program Files\Microsoft Enterprise Library 4.1 - October 2008\Bin) rather than from the source code (Entlib41Src\bin).

By the way, please create a new discussion thread for each different question.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com