locked
Problem with EntLib validation configuration regex pattern RRS feed

  • Question

  • User-1043994744 posted

    There doesn't seem to be a right place in the forums for an EntLib Validation question so I'll try it here.

     I want to validate a 'Middle initials' field. It should be left empty or it should be filled with single, capital letters, each letter followed by a period. If I configure the validation as attributes above my property, then all works fine, but I want to configure the validation rules in the web.config so that they can be easily changed if necessary without a redeployment of the website. Here's how my attributes are configured:

    [ValidatorComposition(CompositionType.Or, Ruleset = "InitialValidation",

    MessageTemplate = "Voorletters moet leeg gelaten worden of gevuld met een series hoofdletters, elke gevolgd door een puntje.")

    , NotNullValidator(Negated = true, Ruleset = "InitialValidation")

    , RegexValidator(@"^([A-Z]\.)+$", Ruleset = "InitialValidation")]

    public string MiddleInitials { get; set; }

    In the web.config the same configuration looks like the following:

    <property name="MiddleInitials">

    <validator messageTemplate="Voorletters moet leeg gelaten worden of gevuld zijn met een series hoofdletters, elke gevolgd door een puntje."

    messageTemplateResourceName="" messageTemplateResourceType=""

    tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    name="Or Composite Validator" />

    <validator negated="true" messageTemplate=" " messageTemplateResourceName=""

    messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    name="Not Null Validator" />

    <validator pattern="^([A-Z]\.)+$" options="None" patternResourceName=""

    patternResourceType="" messageTemplate=" " messageTemplateResourceName=""

    messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

    name="Regex Validator" />

    </property>

    The problem is with the "\.". It doesn't cause any errors but the validation always fails. I've even tried

    "^([A-Z]\\.)+$" and "^([A-Z]\\\\.)+$", nothing seems to be acceptable, the validation always fails.

     

    Does anyone know how to get around this problem?

    Saturday, June 20, 2009 8:46 AM

Answers

  • User-1043994744 posted

    Well I kept on searching and finally found the answer to my problem.

    It had nothing to do with the regex pattern, it should simply be: pattern="^([A-Z]\.)+$".

    The problem was that I had used the Visual Studio add-in to create my configuration, adding an OrCompositeValidator, an NotNullValidator and a RegexValidator to property "MiddleInitials". When you use the OrCompositeValidator, the validators included in it have to be enclosed within that validator. The configuration tool doesn't take that into account and if you don't happen to know that, then you're in for some frustration. So the configuration should look like the following:

    <validator ... name="Or Composite Validator" >

                <validator ... name="NotNullValidator" />

                <validator  pattern="^([A-Z]\.)+$"... name="RegexValidator" />

    </validator>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 20, 2009 4:03 PM

All replies

  • User-1043994744 posted

    Well I kept on searching and finally found the answer to my problem.

    It had nothing to do with the regex pattern, it should simply be: pattern="^([A-Z]\.)+$".

    The problem was that I had used the Visual Studio add-in to create my configuration, adding an OrCompositeValidator, an NotNullValidator and a RegexValidator to property "MiddleInitials". When you use the OrCompositeValidator, the validators included in it have to be enclosed within that validator. The configuration tool doesn't take that into account and if you don't happen to know that, then you're in for some frustration. So the configuration should look like the following:

    <validator ... name="Or Composite Validator" >

                <validator ... name="NotNullValidator" />

                <validator  pattern="^([A-Z]\.)+$"... name="RegexValidator" />

    </validator>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 20, 2009 4:03 PM
  • User-1043994744 posted
    Wow, I hate it when I'm too hasty. The configuration tool works just fine. When using the Or Composite Validator, you have to add the validators to be included in the composite validator TO the composite validator. I added the validators I wanted included in the Or Composite Validator to the property, at the same level as the Or Composite Validator; That's why it initially didn't work. In any case, I think the Validation Application Block is brilliant! And recommend it highly!
    Tuesday, June 23, 2009 3:53 AM