I have a p开发者_Python百科roblem with the validation message for Regular expression. Localised messages appear everywhere except for one field below:
[LocalizedDisplayName("LblWordCount", NameResourceType = typeof(ValidationMessages.Messages))]
[Required(ErrorMessageResourceName = "ErrorFieldRequired", ErrorMessageResourceType = typeof(ValidationMessages.Messages))]
[RegularExpression(@"^[0-9]+$", ErrorMessage = "", ErrorMessageResourceName = "ErrorDigitsOnly", ErrorMessageResourceType = typeof(ValidationMessages.Messages))]
public Int32 WordCount { get; set; }
It doesn't matter what I put in the resx file for "ErrorDigitsOnly" - the default message always kicks in: "The value 'zxzza1' is not valid for Word Count." For instance - message for the [Required] appears correctly.
Any suggestions for that?
Cheers, 303
I have checked the code for spelling mistakes but couldn't find any.
I actually ran across a similar situation myself. Have you tried setting the wordcount validation in another class instead of using the regex pattern in the DataAnnotation Attribute?
For example -
public class EmailAttribute: RegularExpressionAttribute
{
public EmailAttribute() : base(@"[^@\.]+(\.[^@\.]+)*@[^@\.]+(\.[^@\.]+)?(\.[^@\.]{2,})")
{
}
public override string FormatErrorMessage(string name)
{
return Resources.Resources.emailValidation;
}
}
You can then use the attribute like so -
[CustomRequired]
[Email]
public string Email { get; set; }
The advantages of using it this way means that you get strong typing on your resources and it also allows you to write neater validation classes.
Hope it helps!
精彩评论