开发者

EntLib validation syntax for optional field rules?

I have an field, Address2, which is optional. Thus if it is null, no validation rules apply. However, if a value exists, its length cannot be greater that 255 characters.

I have been toying with:

<StringLengthValidator(0, RangeBoundaryType.Inclusive, 255, RangeBoundaryType.Inclusive, MessageTemplate:="Address 2 can be between 0 and 255 characters in length.", Ruleset:="MyRules")> _

But if 开发者_运维技巧it's not present, I still get an error.

Any suggestions?

Thank you.


The following attributes will require that the string length be between 5 and 255 characters if a value is specified (including empty string) or that the string is null.

<ValidatorComposition(CompositionType.Or, Ruleset:="MyRules", MessageTemplate:="Address line 2 must be between 5 and 255 characters")> _
<StringLengthValidator(5, 255, Ruleset:="MyRules")> _
<NotNullValidator(Negated:=True, Ruleset:="MyRules")> _
Public Property Address2() As String


So all Address2 strings must be between 5 and 255 characters unless Address2 is null.


You should decorate the property with an IgnoreNullsAttribute:

<IgnoreNulls>
<StringLengthValidator(0, RangeBoundaryType.Inclusive, ... )>
public string Address2 { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜