jgauffin / griffin.mvccontrib

A contribution project for ASP.NET MVC3
http://blog.gauffin.org/tag/griffin-mvccontrib/
GNU Lesser General Public License v3.0
83 stars 40 forks source link

Attribute not recognized (MVC4): System.ComponentModel.DataAnnotations.EmailAddressAttribute : DataTypeAttribute #33

Closed paolosanchi closed 11 years ago

paolosanchi commented 11 years ago

Hello, the issue is related to the localization library. If i decorate a property with the EmailAddressAttribute (so it is validated as an email) it seems that the library recognize it as its base type (DataTypeAttribute). The validation message is not displayed as untranslated, too, in the pages (the notation with the []). The EmailAddressAttribute class belongs to the Assembly System.ComponentModel.DataAnnotations.dll, v4.0.0.0

Another issue i had is that i can't understand how the translations are automatically inserted in the database.. i found the ID field that i've never displayed (@Html.Display(m=>m.ID) so i expect the library doesn't add it.. I expect that just the text shown somewhere in my application should be translated.

paolosanchi commented 11 years ago

The problem is that in the EmailAddressAttribute the error message is assigned in the contructor, so it's like the error message is specified as a data annotation.. and it is not created in the database because of this: // ----------------------------- // specified a message, do nothing if (attr.ErrorMessage != null && attr.ErrorMessage != WorkaroundMarker) { validators.Add(new DataAnnotationsModelValidator(metadata, context, attr)); continue; } // -----------------------------

Here the code by microsoft:

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); public EmailAddressAttribute() : base(DataType.EmailAddress) { base.ErrorMessage = DataAnnotationsResources.EmailAddressAttribute_Invalid; } public override bool IsValid(object value) { if (value == null) { return true; } string text = value as string; return text != null && EmailAddressAttribute._regex.Match(text).Length > 0; } } }

prvijesh commented 11 years ago

I'm also facing the same issue

jgauffin commented 11 years ago

I love the inconsistnency. If they just could stick to one solution to make it easier to localize the attributes.

jgauffin commented 11 years ago

I'm giving up. I can fix it without using more ASP.NET MVC hacks. I've started to work on an alternative validation library for MVC which will be an drop in replacement.

soosan commented 11 years ago

hats off...