Convert Validator from EVil to DataAnnotation

We were using Evil Validation library for a while, works OK, but doesn’t support WPF/Silverlight and MVC very well. Fortunately, it’s not too hard to convert it to DataAnnotations.

Evil code example:


    class EmailAddress
    {
        public EmailAddress(string value)
        {
            Value = value;
        }

        [ValidateRegex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}", "Invalid recipient email address")]
        public string Value { get; set; }

    }

        public static bool ValidateEmailAddress(string emailAddress)
        {
              return ((new EmailAddress(emailAddress)).IsValid());
        }

To DataAnnotation:


    class EmailAddress
    {
        public EmailAddress(string value)
        {
            Value = value;
        }

        [RegularExpression(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}", ErrorMessage = "Invalid recipient email address")]
        public string Value { get; set; }

    }

       public static bool ValidateEmailAddress(string emailAddress)
        {

            EmailAddress emailToValidate = new EmailAddress(emailAddress);

            // Watchout the last bool flag, indicate whether validate all properties. (What else? just modified one?)
            return Validator.TryValidateObject(emailToValidate, new ValidationContext(emailToValidate, null, null), null, true);

            // For one property model, we can also use this syntax.
            ValidationContext validationContext = new ValidationContext(emailToValidate, null, null);
            validationContext.MemberName = "Value";
            return Validator.TryValidateProperty(emailAddress,
                validationContext
                , null);

        }
Advertisements

8 thoughts on “Convert Validator from EVil to DataAnnotation

  1. I freshly designed my very 1st homepage and I got a lot of ideas by having a look at this domain and its layout!
    Thank you very much! -Ariel

  2. My brother recommended I might luke this blog.
    He used to be totally right. This submit truly made myy day.
    You cann’t consiuder simply how much time I had spent for thbis information! Thanks!

  3. Son regard allait lentement des beaux cheveux de sa fille, qui la
    couvraient comme d’une mantille, à la figure d’Henri, qu’elle observait avec une inexprimable curiosité.

  4. Wir wollen jetzt den Hintergrund und vor allem den genauen Ablauf ermitteln”,
    sagte Nickelsen Die Beamten prüften außerdem einen Verstoß gegen das
    Betäubungsmittelgesetz.

  5. Je fais régime sport et je n’arrive pas a contrôler ce
    ventre qui s’arrondi de semaine en semaine les produits que l’on trouve en pharmacie sont ils
    vraiment efficaces.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s