Entlib validation to syntax to accept only numeric month numbers?

Posted by ElHaix on Stack Overflow See other posts from Stack Overflow or by ElHaix
Published on 2010-03-18T15:42:32Z Indexed on 2010/03/18 17:41 UTC
Read the original article Hit count: 613

Filed under:
|
|
|

I've got an enum defined as such:

Private Enum AllowedMonthNumbers
    _1
    _2
    _3
    _4
    _5
    _6
    _7
    _8
    _9
    _10
    _11
    _12
End Enum

Then a property validator defined as:

<TypeConversionValidator(GetType(Int32), MessageTemplate:="Card expiry month must be numeric.", Ruleset:="CreditCard")> _
<EnumConversionValidator(GetType(AllowedMonthNumbers), MessageTemplate:="Card expiry month must be between 1 and 12.", Ruleset:="CreditCard")> _

The validation expects "_#", as when I remove the TypeConversionValidator, it passes with setting the value to "_3" or any other number in the enum.

What I need is for this to only accept b/t 1-12, and simply having numeric values in the enum won't work.

Any tips?

Thanks.

UPDATE

I replaced the EnumConversionValidator with a RangeValidator, and attempting to set the parameter to "1", but received the following error:

<RangeValidator(1, RangeBoundaryType.Inclusive, 12, RangeBoundaryType.Inclusive, MessageTemplate:="..."> 

However that's now giving me the following error:

System.Web.Services.Protocols.SoapException : System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ArgumentException: Object must be of type Int32.
   at System.Int32.CompareTo(Object value)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeChecker`1.IsInRange(T target)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator`1.DoValidate(T objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validator`1.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValueAccessValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validators.GenericValidatorWrapper`1.DoValidate(T objectToValidate, Object currentTarget, String key, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validator`1.Validate(T target, ValidationResults validationResults)
   at Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate[T](T target, String[] rulesets)
   at ....

© Stack Overflow or respective owner

Related posts about EntLib

Related posts about validation