Entlib 验证语法是否只接受数字月份?
我有一个这样定义的枚举:
Private Enum AllowedMonthNumbers
_1
_2
_3
_4
_5
_6
_7
_8
_9
_10
_11
_12
End Enum
然后属性验证器定义为:
<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")> _
验证需要“_#”,因为当我删除 TypeConversionValidator 时,它会通过将值设置为“_3”或枚举中的任何其他数字。
我需要的是仅接受 b/t 1-12,并且仅在枚举中包含数字值是行不通的。
有什么建议吗?
谢谢。
更新
我用 RangeValidator 替换了 EnumConversionValidator,并尝试将参数设置为“1”,但收到以下错误:
<RangeValidator(1, RangeBoundaryType.Inclusive, 12, RangeBoundaryType.Inclusive, MessageTemplate:="...">
但是,现在出现以下错误:
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 ....
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 ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RangeValidator 只能处理基本类型,例如 int、float、double、decimal。因为您在构造函数中提供了一个整数,所以它会尝试将该 int 与您的枚举相匹配,这将失败,因为 VAB 使用
Object.Compare(object)
方法。尝试使用不必将数字范围定义为枚举的设计。尝试使用简单的整数字段。痛苦少得多。
The
RangeValidator
can only handle primitive types such as int, float, double, decimal. Because you supply an integer in the constructor it is trying to match that int to your enum, which will fail, because VAB uses theObject.Compare(object)
method.Try using a design where you don't have to define a number range as enum. Try using a simple integer field instead. Much less painful.