Entlib5 string validation

假如想象 提交于 2019-12-24 16:53:25

问题


How can I validate some string if this is the rule:

- can be empty
- if not empty -> max length: 30

I know this two ways:

[IgnoreNulls]
[StringLengthValidator(30)]

or

[ValidatorComposition(CompositionType.Or)]
[StringLengthValidator(30)] 
[NotNullValidator(Negated=true)]

but is there a way NOT to use IgnoreNulls or Composition.Or (have problems: Entlib5 Validation [IgnoreNull] throws exception while adding objects to list)


回答1:


I've solved this issue. I couldn't use Composition, NotNull or IgnoreNull validators. What I did is:

private string _address = string.Empty; // IMPORTANT!

[StringLengthValidator(30, "Max. 30 chars")]
public string Address {
    get { return _address; }
    set { _address = value; }
}

Now, in first call field _address is not null, it's empty string and IgnoreNulls annotation is now not needed. StringLengthValidator now checks only if Address is <=30 characters.



来源:https://stackoverflow.com/questions/7832973/entlib5-string-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!