DomainValidator Domain from resource file

Topics: Validation Application Block
Mar 17, 2009 at 10:32 PM
Edited Mar 17, 2009 at 10:43 PM
hi experts

is there anyway where i can retrieve my list of scripts which i want to validate from the resource file?

rather than having to change every line of where a DomainValidator exists for every input which requires validation, i would like it so that i can just change the list in a resource file

below is what i have now

    Dim _PageTitle As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property PageTitle() As String

        Get
            Return _PageTitle
        End Get

        Set(ByVal _value As String)
            _PageTitle = _value
        End Set

    End Property

would like to change it to.....

    Dim _PageTitle As String = Nothing
    <DomainValidator(Domain:=Resources.ice.script_list, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property PageTitle() As String

        Get
            Return _PageTitle
        End Get

        Set(ByVal _value As String)
            _PageTitle = _value
        End Set

    End Property

any one know how i this can be done?

Mar 18, 2009 at 4:13 AM
I'm thinking you could create a custom domain validator,  basically just wrapping the DomainValidator.  Implement the logic of getting the values from the resource file in the constructor.  In the constructor, you would also just be creating a domainvalidator class passing those values.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 18, 2009 at 4:18 AM
i'm a bit lost,
how can i use custom domain validator?

or question is, how do i wrap it in the domain validator?

thanks
lindows
Mar 18, 2009 at 4:27 AM
You would create a custom validator and wrap within it the domain validator.  It's like this: you create a new validator, but inside, you're just really creating a domainvalidator of entlib.  Check out the documentation on how to create a custom validator.  You can also look at the quickstart for validation for the sample code.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com




Mar 18, 2009 at 4:32 AM
would you have some sample source code as i don't have the sample stuff except the dll files
Mar 18, 2009 at 4:43 AM
Something like this:  (just take care of the rest of the implementations :)


[

ConfigurationElementType(typeof(CustomValidatorData))]
public class CustomDomainValidator : Validator<string>
{
    
DomainValidator<string> validator;

    
public CustomDomainValidator()
        :
base(string.Empty, string.Empty)
    {
            
IEnumerable<string> domains = //get from resource file.
            
validator = new DomainValidator<string>(domains, false);
 
   }

 

 

    protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
    {
            validator.Validate(objectToValidate, validationResults);
    }

    
protected override string DefaultMessageTemplate
    {
            
get { throw new NotImplementedException(); }
    }
}

 


I haven't tested this yet, but let me know if you encountered any problem.  I'll get back to you as soon as I can, need to attend a brownbag session.  By the way, you can have the source code if you installed entlib.  It comes with the source code installer which also includes the quickstarts. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com


 

Mar 18, 2009 at 5:21 AM
Edited Mar 18, 2009 at 5:23 AM
ok i've tried your code and no i'm encountering errors

here is my validation class

Imports Microsoft.VisualBasic
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Imports Resources

Public Class ss_validation

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

    Dim _PageTitle As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property PageTitle() As String

        Get
            Return _PageTitle
        End Get

        Set(ByVal _value As String)
            _PageTitle = _value
        End Set

    End Property

    Dim _MetaKeywords As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property MetaKeywords() As String

        Get
            Return _MetaKeywords
        End Get

        Set(ByVal _value As String)
            _MetaKeywords = _value
        End Set

    End Property

    Dim _MetaDescription As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property MetaDescription() As String

        Get
            Return _MetaDescription
        End Get

        Set(ByVal _value As String)
            _MetaDescription = _value
        End Set

    End Property

    Dim _FileName As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property FileName() As String

        Get
            Return _FileName
        End Get

        Set(ByVal _value As String)
            _FileName = _value
        End Set

    End Property

    Dim _SearchQuery As String = Nothing
    <DomainValidator("<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>", MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", negated:=True)> _
    Public Property SearchQuery() As String

        Get
            Return _SearchQuery
        End Get

        Set(ByVal _value As String)
            _SearchQuery = _value
        End Set

    End Property

End Class

as you can see i require the DomainValidator in all inputs to check for script injections, but rather updating every single line, it would be must easier to do it via the resource file

Resources.ice.scripts_to_validate = "<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>" from the resource file

rather than having to redo this for every page i am going to validate,  i'm after an implementation where i can just pass Resources.ice.scripts_to_validate into the DomainValidator inside this class ONLY and not other code behind files...

i've tried couple of other methods, but they all lead to dead ends
Mar 18, 2009 at 6:45 AM
Could you clarify, did you use the customvalidator?  As I can see, you're still decorating your property with the DomainValidator attribute.  What's the issue if it's going to be used with other code behind files? 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 18, 2009 at 6:49 AM
sorry, my mistake

this is what i have so far

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

<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class ss_validation : Inherits DomainValidator(Of String)
    Private Shared script_list As List(Of String) = New List(Of String)(New String() {Resources.ice.scripts_to_validate})

    Public Sub New()
        MyBase.New(scripts)
    End Sub

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    <DomainValidator(script_list, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", Negated:=True)> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

the script_list is storing the value from resource file, but not sure how can i pass it into the domain validator



Mar 18, 2009 at 7:07 AM
You already have the script_list inside the class, all you need to do is pass use it when creating the domainvalidator during the instantiation of your ss_validation class, just like what I did in my sample code.   Declare a global variable inside your class of typ DomainValidator<string> . 
ConfigurationElementType(typeof(CustomValidatorData))]
public class CustomDomainValidator : Validator<string>
{
     DomainValidator<string> validator;

 In the constructor,  instantiate that variable passing the script_list variable and a value of false to indicate that it is not negated. 

      public CustomDomainValidator()
        :
base(string.Empty, string.Empty)
      {
            
validator = new DomainValidator<string>(script_list, false);
 
     } 

Just make sure that each item in the script_list represents a single string which you want to be valid. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 18, 2009 at 7:19 AM
i'm a bit lost of where things go where now... :S

i take it this stuff will be in a separte file from my existing ss_validation file?
ConfigurationElementType(typeof(CustomValidatorData))]
public class CustomDomainValidator : Validator<string>
{
     DomainValidator<string> validator;

and
      public CustomDomainValidator()
        :
base(string.Empty, string.Empty)
      {
            
validator = new DomainValidator<string>(script_list, false);
 
     }

inside my existing validation file?

also am i not able to use Private Shared script_list As List(Of String) = New List(Of String)(New String() {Resources.ice.scripts_to_validate})

got this sample from the quickstartup

    Private Shared stateCodes As List(Of String) = New List(Of String)(New String() { _
            "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", _
            "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", _
            "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", _
            "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", _
            "SD", "TN", "TX", "UT", "VT", "VI", "VA", "WA", "WV", "WI", "WY"})



Mar 18, 2009 at 7:41 AM
The CustomDomainValidator is the same as your ss_validation.  You should implement the same logic inside the constructor of your ss_validation file as with

public CustomDomainValidator()
        :
base(string.Empty, string.Empty)
      {
            
validator = new DomainValidator<string>(script_list, false);
 
     }


and your ss_validation file should also have a private field of type DomainValidator<string>


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 18, 2009 at 8:17 AM
Edited Mar 18, 2009 at 8:58 AM
ok this is what i have now...

<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class ss_validation : Inherits Validator(Of String)
    Private validator As DomainValidator(Of String)
    Private script_list As List(Of String) = New List(Of String)(New String() {Resources.ice.scripts_to_validate})

    Public Sub New()
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    <DomainValidator(MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="validation_script_injection", Negated:=True)> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

i know i'm missing something,
DomainValidator checks the script_list?
or maybe i need to do some fancy integration with the CustomValidatorData with the DomainValidator?


Mar 18, 2009 at 9:05 AM
You don't need to bother with the CustomValidatorData.  The DomainValidator will use the items from the script_list you passed in to check for the valid/invalid values.  In your case, if your script_list contains 3 items namely, "valid1", "valid2", "valid3", it will tag these strings as invalid since you passed in True for the negated parameter.  Have you already tried to use this validator in your current implementation?  


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Mar 18, 2009 at 9:21 AM
the reason why i'm negated the domain validator is that i don't want
"<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>"
to be pass through

and now i just realized

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

    End Sub

    Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get

        End Get
    End Property

What do i need to put into these?
NOTE: the 2 subs are also on the Public Class ss_validation : Inherits Validator(Of String) file

i think this is what i'm missing to do the validation
Mar 18, 2009 at 9:37 AM
See the implementation of my CustomDomainValidator of the DoValidate:  

 protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
    {
            validator.Validate(objectToValidate, validationResults);
    }

The DoValidate method of your ss_validation class will just call Validate method of the DomainValidator.  That's the whole point of wrapping the DomainValidator class.  We just modify the way it gets the domain values, we're not modifying any of its other implementation.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 18, 2009 at 9:42 AM
so whats this if i want the message from Resources.ice.validation_script_injection

   Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get

        End Get
    End Property
Mar 18, 2009 at 9:50 AM
Just return Resources.ice.validation_script_injection if you want it to be the default error message.

 Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get
            Return Resources.ice.validation_script_injection 
        End Get
    End Property 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 18, 2009 at 10:14 AM
ok now it looks like this

<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class ss_validation : Inherits Validator(Of String)
    Private validator As DomainValidator(Of String)
    Private script_list As List(Of String) = New List(Of String)(New String() {"<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>"})

    Public Sub New()
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

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

    Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get
            Return Resources.ice.validation_script_injection
        End Get
    End Property

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    <DomainValidator()> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

so what do i put in the <DomainValidator()> _ so that it validates with the override sub?
or am i using the entire thing incorrect?


Mar 18, 2009 at 10:35 AM
You shouldn't be using the DomainValidator to decorate your property.  Use ss_validation class that you've just created.  


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 18, 2009 at 10:55 AM
Edited Mar 18, 2009 at 11:05 AM
so you're saying that this is all i need and it should work?

<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class ss_validation : Inherits Validator(Of String)
    Private validator As DomainValidator(Of String)
    Private script_list As List(Of String) = New List(Of String)(New String() {"<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>"})

    Public Sub New()
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

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

    Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get
            Return Resources.ice.validation_script_injection
        End Get
    End Property

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

i've just ran this, it doesn't seem to do any DomainValidation... any ideas?


Mar 18, 2009 at 11:06 AM

The validator won't really get triggered, you didn't decorate the PageName property with the ss_validation validator.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 

Mar 18, 2009 at 11:09 AM
how do i decorate the PageName property with the ss_validation validator
Mar 18, 2009 at 11:17 AM
Edited Mar 18, 2009 at 11:27 AM
Sorry, my mistake.  You have to specify it in the configuration file.  Use the entlib configuration tool.  Are you familiar with this method?


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 18, 2009 at 11:34 AM
no, i've only been doing it in this class and linking it with code behinds
Mar 18, 2009 at 11:48 AM

I missed adding the parameter NameValueCollection to the ss_validation's constructor.

Public Sub New(NameValueCollection attributes)
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

You can just ignore this parameter, but it's necessary that you have this constructor as this is the constructor that entlib looks for. 
With regards to the specifying the validator, you really should specify it in the config file.  Actually, putting the validation rules in the config file is better since this would remove the necessity of rebuilding your source code when you decided to change the validation rules.

Here's how to use it.  If you're using the 4.1 version, when you right click the config file, there is an option "Edit Enterprise Library Configuration".  Select it and then you'll see the config editor.
Right click on the node below the Enterprise Library Configuration, select New -> Validation Application Block.  For the additional steps, please refer to the documentation, under the Validation Application Block -> Entering Configuration Information.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 

 

Mar 18, 2009 at 12:14 PM
oh i see what you mean,

the reason why we use stuff out of resource is that we have many copies of this resource file but in different languages, so all error, labels, text, messages...etc.. uses the same key but different resource file name

i've now changed

  Public Sub New()
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

to

    Public Sub New(ByVal NameValueCollection As Attribute)
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

i'm still stuck on your comment here
"The validator won't really get triggered, you didn't decorate the PageName property with the ss_validation validator." my head is spinning with this already, i swear its something easy but i'm just not seeing this :(

sample code please of the decorate

    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplateResourceType:=GetType(ice), MessageTemplateResourceName:="page_name_callout")> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property


Mar 18, 2009 at 12:33 PM
Scrap that, you can't specify that in the attribute.  Just associate the ss_validator in the configuration file.  Refer to the documentation on how to do this, under the Validation Application Block -> Entering Configuration Information.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 18, 2009 at 1:01 PM
Edited Mar 18, 2009 at 1:44 PM
i've now added this to the web config.... still not validating

  <configSections>
      <section name="ss_validation" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
..... 
</configSections>

what am i missing???

something from the ss_validation class or something in the web.config file?
Mar 18, 2009 at 1:54 PM
Were you able to add the validation application block the validation rules for the PageName property? If so, please post the validation section here.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 19, 2009 at 1:40 AM
i added the validation application block using the enterprise lib editor
but once i added it
the web.config is
<section name="validation" .... should i be changing that to ss_validation?

and i'm not sure how to add the validation rules for the PageName property?
Mar 19, 2009 at 4:48 AM
No, the section name is "validation".  Don't change that.  After adding the Validation Application Block, right click on its node, select New, select Type.  The type you should specify is the type you want to validate.  I've just noticed, why are you combining your ss_validation and your business object?  I thought the ss_validation is your custom validator, but looking from your previous post, I noticed that the PageName is also a member your ss_validation class.  Separate your business object/the class you want to validate from your validator.  Once you have done this, you can easily follow the steps in the documentation.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 19, 2009 at 4:57 AM
Edited Mar 19, 2009 at 9:13 AM
i have sent an email to entlib.support@avanade.com

i have created a small repo of the topic being discussed, are you able to make the following fixes so i know what i am doing wrong

thanks in advance
Mar 19, 2009 at 5:25 AM
If I fixed it, I'd still explain the details here, it will be better for you if you do it on your own so you will get familiar with it.  Now, the first step is to move out the PageName and PageTitle property to a different class, let's call it BusinessPage.  Build your solution.  Edit your configuration file using the config tool.  You've already added the Validation Application block, now the next thing to do is to right click on the Validation Application Block, select New, select Type.  You'll see the type picker, browse to the bin/debug directory of the project where your BusinessPage class is.  If it's a class library, look for the "nameoftheproject.dll", if it's an executable, look for the "nameoftheproject.vshost.exe".  After selecting the correct assembly, select from the list below the class you want to validate.  Click Ok.  Right click on the class you've just added, select New -> Rule Set.  Right click on the Rule Set,  select Choose Members.  You will then get a window that allows you to select properties and methods of the BusinessPage.  Check the PageName property, click Ok.  Your property will then be added directly under the BusinessPage node, right click on the PageName property, selelect New -> Custom Validator, click Ok.  Click on the Custom Validator node, look for the Type property in the Properties window.  Click on the ellipsis button, browse again for the dll or .exe where the ss_validation class is defined, just like what you did when selecting the BusinessPage class.  Also, click on the BusinessPage node, set the Default Rule Set property to the Rule Set you've just created.  That's it.  You may think these are quite long steps, but they are not really complicated.  Using the configuration tool is very straightforward, you'll get the hang of it at once.   Please try to follow this first, it's in my best interest to get everyone in this forum to get them to learn how to use enterprise library.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 19, 2009 at 5:41 AM
ok, i've moved the stuff and now its into 2 files

CustomDomainValidation.vb
Imports Microsoft.VisualBasic
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Configuration

<ConfigurationElementType(GetType(CustomValidatorData))> _
Public Class CustomDomainValidation : Inherits Validator(Of String)
    Private validator As DomainValidator(Of String)
    Private script_list As List(Of String) = New List(Of String)(New String() {"<script />", "</ script>", "<script>", "</script>", "<%", "%>", "<!", "!>", "<noscript>", "</noscript>", "<?php", "?>"})

    Public Sub New(ByVal NameValueCollection As Attribute)
        MyBase.New(String.Empty, String.Empty)
        validator = New DomainValidator(Of String)(script_list, True)
    End Sub

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

    Protected Overrides ReadOnly Property DefaultMessageTemplate() As String
        Get
            Return "Validation has Failed"
        End Get
    End Property

End Class

BusinessPage.vb

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

Public Class BusinessPage
    Dim _PageName As String = Nothing
    <StringLengthValidator(1, 20, MessageTemplate:="String must be no more than 20 characters")> _
    Public Property PageName() As String

        Get
            Return _PageName
        End Get

        Set(ByVal _value As String)
            _PageName = _value
        End Set

    End Property

    Dim _PageTitle As String = Nothing
    Public Property PageTitle() As String

        Get
            Return _PageTitle
        End Get

        Set(ByVal _value As String)
            _PageTitle = _value
        End Set

    End Property

End Class

if this is correct please instruct me on how to build solution, so i can add the dll file from VAB using the editor
Mar 19, 2009 at 5:45 AM
What I meant with build is from the Build menu, Build Solution - Ctrl + Shift + B


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com 
Mar 19, 2009 at 5:53 AM
is there any problem with the separation of the class?

also regarding
"next thing to do is to right click on the Validation Application Block, select New, select Type.  You'll see the type picker, browse to the bin/debug directory of the project where your BusinessPage class is.  If it's a class library, look for the "nameoftheproject.dll", if it's an executable, look for the "nameoftheproject.vshost.exe". "

when i click new from VAB, select New, select Type. it pops up with the assemblies picker, question is what do i pick?
Mar 19, 2009 at 6:10 AM
The classes are fine.  As I have said, pick the assembly where you define the class you want to validate.  " If it's a class library, look for the "nameoftheproject.dll", if it's an executable, look for the "nameoftheproject.vshost.exe".


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com