问题
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