![]() | |
Creating Validator Classes |
This topic contains the following sections.
The example shows a simple validator.
The BcfMandatoryAttribute will cause BCF Editor to show an error if the property is not used as parameter.
public class StringLengthValidator : BcfValidatorBase { [BcfMandatory] public string StringValue { get; set; } [BcfMandatory] public int MaxLength { get; set; } public override BcfValidatorMessage Validate() { if (StringValue == null) return null; if (StringValue.Length <= MaxLength) return null; return new BcfValidatorMessage("Too many characters. Allowed are {0:#,##0} maximum.", MaxLength); } }
The example shows how to add additional info to validator messages.
The BcfMandatoryAttribute will cause BCF Editor to show an error if the property is not used as parameter.
public enum Severity { Error, Warning, Hint, }
public class MyValidatorMessage : BcfValidatorMessage { public MyValidatorMessage(Severity severity, string message, params object[] formatParameters) : base(message, formatParameters) { Severity = severity; } public Severity Severity { get; private set; } public override int GetHashCode() { unchecked { return base.GetHashCode() + 21 * Severity.GetHashCode(); } } public override bool Equals(object obj) { return base.Equals(obj) && ((MyValidatorMessage)obj).Severity == Severity; } }
public class NotNullValidator : BcfValidatorBase { [BcfMandatory] public object Value { get; set; } [BcfMandatory] public Severity Severity { get; set; } public override BcfValidatorMessage Validate() { if (Value != null) return null; return new MyValidatorMessage(Severity, "No value"); } }