ASP.NET MVC - "Validation type names must be unique."

I am using ASP.NET MVC 3 and data annotations in my model and want to receive error messages from the database. Therefore, I wrote the inherited attributes:

public class LocalizedRequiredAttribute : RequiredAttribute { public LocalizedRequiredAttribute(){} public override string FormatErrorMessage(string name) { return GetByKeyHelper.GetByKey(this.ErrorMessage); } } public class LocalizedRegularExpressionAttribute : RegularExpressionAttribute { public LocalizedRegularExpressionAttribute(string pattern) : base(pattern){} public override string FormatErrorMessage(string name) { return GetByKeyHelper.GetByKey(this.ErrorMessage); } } 

I wrote 2 β€œadapters” for these attributes to enable client checks, for example:

 public class LocalizedRequiredAttributeAdapter : DataAnnotationsModelValidator<LocalizedRequiredAttribute> { public LocalizedRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, LocalizedRequiredAttribute attribute) : base(metadata, context, attribute) { } public static void SelfRegister() { DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter)); } public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() { return new[] { new ModelClientValidationRequiredRule(ErrorMessage) }; } } 

And in my global.asax, I have two lines:

  LocalizedRegularExpressionAttributeAdapter.SelfRegister(); LocalizedRequiredAttributeAdapter.SelfRegister(); 

I get an exception. Validation type names in unobtrusive client validation rules must be unique. The following type of validation has been examined more than once: required "when my model displays HTML for this property:

  [LocalizedRequired(ErrorMessage = "global_Required_AccountName")] [LocalizedRegularExpression(User.ADAccountMask, ErrorMessage = "global_Regex_AccountName")] public string AccountName { get; set; } 

What's wrong?

+1
source share
3 answers

I basically had the same problem, and I was able to solve it with the following code snippet:

 using System; using System.Web.Mvc; 

And ValidationRule:

 public class RequiredIfValidationRule : ModelClientValidationRule { private const string Chars = "abcdefghijklmnopqrstuvwxyz"; public RequiredIfValidationRule(string errorMessage, string reqVal, string otherProperties, string otherValues, int count) { var c = ""; if (count > 0) { var p = 0; while (count / Math.Pow(Chars.Length, p) > Chars.Length) p++; while (p > 0) { var i = (int)(count / Math.Pow(Chars.Length, p)); c += Chars[Math.Max(i, 1) - 1]; count = count - (int)(i * Math.Pow(Chars.Length, p)); p--; } var ip = Math.Max(Math.Min((count) % Chars.Length, Chars.Length - 1), 0); c += Chars[ip]; } ErrorMessage = errorMessage; // The following line is where i used the unique part of the name // that was generated above. ValidationType = "requiredif"+c; ValidationParameters.Add("reqval", reqVal); ValidationParameters.Add("others", otherProperties); ValidationParameters.Add("values", otherValues); } } 

Hope this helps.

+1
source

I assume that maybe a line that reads:

  DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(LocalizedRequiredAttribute), typeof(RequiredAttributeAdapter)); 

Must read:

  DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(LocalizedRequiredAttribute), typeof(LocalizedRequiredAttributeAdapter)); 
0
source

The correct answer is in a similar question: fooobar.com/questions/1345053 / ...

Basically, you cannot have both attributes at the same time

0
source

Source: https://habr.com/ru/post/1345055/


All Articles