Entlib5字符串验证 [英] Entlib5 string validation
问题描述
如果这是规则,我该如何验证某些字符串:
How can I validate some string if this is the rule:
- can be empty
- if not empty -> max length: 30
我知道这两种方式:
[IgnoreNulls]
[StringLengthValidator(30)]
或
[ValidatorComposition(CompositionType.Or)]
[StringLengthValidator(30)]
[NotNullValidator(Negated=true)]
但是有一种方法可以不使用IgnoreNulls或Composition或(有问题: Entlib5验证[IgnoreNull]抛出将对象添加到列表时出现异常)
but is there a way NOT to use IgnoreNulls or Composition.Or (have problems: Entlib5 Validation [IgnoreNull] throws exception while adding objects to list)
推荐答案
我已经解决了这个问题。我无法使用Composition,NotNull或IgnoreNull验证器。我所做的是:
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; }
}
现在,在第一个调用字段_address不为空,它是空字符串,并且现在不需要IgnoreNulls批注。现在,StringLengthValidator仅检查地址是否为< = 30个字符。
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.
这篇关于Entlib5字符串验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!