DomainValidator - How to use Enums?

Topics: Validation Application Block
Jul 22, 2009 at 2:50 AM

Hi,

I have an enum as follows:

    public enum MarketingType
    {
        Unknown,
        Mail,
        Email,
        Decline
    }

I then have a property as follows:
        [DomainValidator(new MarketingType[] { MarketingType.Email, MarketingType.Mail, MarketingType.Decline}, MessageTemplate = "Please select a valid Marketing Contact Code")]
        public MarketingType MarketingContactCode { get; set; }

What I want to do is limit the valid values using a DomainValidator. However, the value to set is provided from a webform and I suspect the validation is failing because the value is read from a combobox (string). How can I implement this behavior?
Thanks in advance!
Jul 22, 2009 at 11:31 AM

Hi,

Do you have specific reason for using enums in the domain instead of strings as shown in the documentation? Yes, the DomainValidator compares the value that is being validated to the values defined as its domain. If it falls under the values in the domain it is valid else not. Actually, I haven't tried using a enum against a DomainValidator, So I would suggest, try it first using strings:

        [DomainValidator("Email", "Mail", "Decline", MessageTemplate = "Please select a valid Marketing Contact Code.")]
        public string MarketingContactCode { get; set; }

so from the combobox if you read it as string you can assign it directly.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

Jul 23, 2009 at 12:30 AM

I think it shouldn't really make a difference based on the way the Domain validator is defined, but from what I gather using reflector the comparison is failing due to a type conversion issue (It might be trying to compare an Enum value against a string value).

We want to use enums so standardize the values used for that property given that it might come from different data sources. In this case, the values are just 3 or 4 but in other instances though the property can take several different values, the valid ones depend on the application providing them. If we can't use enums then it'd be hard to achieve both things at the same time should things change in the future.

 

 

 

 

Jul 23, 2009 at 3:56 AM

Are you using PropertyProxyValidator?  If you are, it has an event ValueConvert which executes before the validation occurs.  Inside that method, you can parse the value of the selected item in the combobox available in the ValueConvertArgs parameter's ValueToConvert property to the enumeration type.  Assign the parsed value to the ConvertedValue property. Place that inside a try catch block and if it throws an exception, that means the selected value in the combobox is not a valid value since it cannot be converted to the enumeration type. 

I'm wondering though why would you put a value in the dropdownlist if it doesn't correspond to an enumeration value. 

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

Jul 23, 2009 at 7:13 AM
Edited Jul 24, 2009 at 12:14 AM

"I'm wondering though why would you put a value in the dropdownlist if it doesn't correspond to an enumeration value."

The value corresponds to the enumeration, but for this particular application we don't want it as a valid value. We want people to choose a valid value and not leave the default as set by the framework.

In any case, the ServerSideValidator has a bug when integrating dropdown lists. It relies on a wrong method to set the value of a dropdown list and thus the value to validate never changes. It's always the default values as set by the framework.

I already solved the issue by implementing my own domain validator that does take Enums into account.

Jul 23, 2009 at 7:58 AM

I see, glad you already solved your issue.  But the entlib Domain Validator can already handle Enumerations.  You can verify it just by looking at the source code (DomainValidator<T>).  You can also try out the steps I mentioned above.

 

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

Jul 23, 2009 at 9:12 PM

It doesn't. It seems it does, but it doesn't. That's why I posted this. And that's why I had to write my own custom validator. The example in MSDN uses the most simple case (strings) which always work.

Jul 27, 2009 at 5:33 AM

I re-read your initial post and found out why you said the domain validator doesn't work for enums.  You should have specified the valid enumerations by separating them with commas.  What you did was added them in an array of enumeration.

[DomainValidator(MarketingType.Email, MarketingType.Mail, MarketingType.Decline, MessageTemplate="....")]

It fails because the domainvalidator was trying to compare if the array is equal to an enumeration.

 

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