Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the DataTypeAttribute validation working in MVC2?

As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC v1. For example,

public class Model
{
  [DataType("EmailAddress")]
  public string Email {get; set;}
}

In the codes above, the Email property will not be validated in MVC v1. Is it working in MVC v2?

like image 339
wenqiang Avatar asked Mar 06 '10 05:03

wenqiang


4 Answers

[DataType("EmailAddress")] doesn't influence validation by default. This is IsValid method of this attribute (from reflector):

public override bool IsValid(object value)
{
    return true;
}

This is example of custom DataTypeAttribute to validate Emails (taken from this site http://davidhayden.com/blog/dave/archive/2009/08/12/CustomDataTypeAttributeValidationCustomDisplay.aspx):

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple  = false)]
public class EmailAddressAttribute : DataTypeAttribute
{
    private readonly Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.Compiled);

    public EmailAddressAttribute() : base(DataType.EmailAddress)
    {

    }

    public override bool IsValid(object value)
    {

        string str = Convert.ToString(value, CultureInfo.CurrentCulture);
        if (string.IsNullOrEmpty(str))
            return true;

        Match match = regex.Match(str);   
        return ((match.Success && (match.Index == 0)) && (match.Length == str.Length));
    }
}
like image 144
LukLed Avatar answered Nov 12 '22 08:11

LukLed


Like LukLed pointed out, DataTypeAttribute doesn't do any validation by default. But it does influence templates regarding how the data is presented.

For example if you call Html.DisplayFor() method on a model that has DataType(DataType.EmailAddress) attribute, it'll format its value with <a href="mailto:{0}">{0}</a> (at least in MVC RC2).

like image 37
Çağdaş Tekin Avatar answered Nov 12 '22 08:11

Çağdaş Tekin


Alternatively, you can directly use RegularExpression attribute on your field instead of creating your own attribute that is in the end going to check for a regex matching.

[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = PaErrorMessages.InvalidEmailAddress)]
public string Email { get; set; }
like image 9
Etienne Avatar answered Nov 12 '22 08:11

Etienne


Starting from .NET 4.5 there is EmailAddressAttribute, which has a correct implementation of the IsValid method. So if you're targeting .NET 4.5, then for validation please consider using the EmailAddressAttribute instead of a custom one. For example,

public class Model
{
    [EmailAddress(ErrorMessage = "INVALID EMAIL")]
    public string Email {get; set;}
}

And if you're curious about the implementation of EmailAddressAttribute, then here is the decompiled (using JetBrains dotPeek decompiler) source of the class:

using System;
using System.ComponentModel.DataAnnotations.Resources;
using System.Text.RegularExpressions;

namespace System.ComponentModel.DataAnnotations
{
  [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
  public sealed class EmailAddressAttribute : DataTypeAttribute
  {
    private static Regex _regex = new Regex("^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled);

    static EmailAddressAttribute()
    {
    }

    public EmailAddressAttribute()
      : base(DataType.EmailAddress)
    {
      this.ErrorMessage = DataAnnotationsResources.EmailAddressAttribute_Invalid;
    }

    public override bool IsValid(object value)
    {
      if (value == null)
        return true;
      string input = value as string;
      if (input != null)
        return EmailAddressAttribute._regex.Match(input).Length > 0;
      else
        return false;
    }
  }
}
like image 2
ajukraine Avatar answered Nov 12 '22 10:11

ajukraine