Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
paolosanchi opened this issue Jan 26, 2013 · 5 comments

Comments

@paolosanchi
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link

I'm also facing the same issue

@jgauffin
Copy link
Owner

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

@jgauffin
Copy link
Owner

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
Copy link

soosan commented Feb 15, 2013

hats off...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants