!Extending Validators

Create a Custom Validator

Create Extended RuleValidator Class

public class EmailAddressValidator<T> : RuleValidator<T,string>
{
    private readonly string VALID_EMAIL_REGEX = @"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})";

    public EmailAddressValidator()
    {
        MessageStoreName = "MyCompanyValidationMessages";
    }

    public override object[] Parameters
    {
        get { return new object[]{}; }
    }

    public override ValidationResult Validate(RuleValidatorContext<T, string> context)
    {
        if (context.PropertyValue == null)
        {
            return null;
        }
        else
        {
            Regex emailRegex = new Regex(VALID_EMAIL_REGEX);
            return Evaluate(emailRegex.IsMatch(context.PropertyValue), context);
        }
    }
}



Create Extension Class for DSL

public static class EmailAddressValidatorExtensions
{
    public static ActionJoinBuilder<T, string> IsEmailAddress<T>(this IRuleBuilder<T, string> expression)
    {
        expression.RegisterValidator(new EmailAddressValidator<T>());
        return expression.JoinBuilder;
    }
}

MessageStore

Create your own or embed the message in the class.

Optional How to create and configure a new MessageStore

TODO: Document creating a Custom Message Store with the naming conventions

Last edited Jan 22, 2010 at 9:48 PM by TechnoAg, version 1

Comments

No comments yet.