Entlib5字符串验证

时间:2011-10-20 08:17:39

标签: c# wpf validation enterprise-library

如果这是规则,我该如何验证一些字符串:

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

我知道这两种方式:

[IgnoreNulls]
[StringLengthValidator(30)]

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

但有没有办法不使用IgnoreNulls或Composition.Or(有问题:Entlib5 Validation [IgnoreNull] throws exception while adding objects to list

1 个答案:

答案 0 :(得分:1)

我已经解决了这个问题。我无法使用Composition,NotNull或IgnoreNull验证器。我做的是:

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

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

现在,在第一个调用字段_address不为null时,它是空字符串,现在不需要IgnoreNulls注释。 StringLengthValidator现在只检查Address是否为< = 30个字符。