38

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: required

This is referring to the EmailAddress property, here:

public class LoginModel
{
    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    [AdditionalMetadata("Style", "Wide")]
    public string EmailAddress { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    [AdditionalMetadata("Style", "Wide")]
    public string Password { get; set; }
}

I'm not using the same type of validation rule twice here. This works fine locally, but not when deployed to the server. What's the deal?

I did add a reference to DataAnnotationExtensions (http://dataannotationsextensions.org), could that be causing an issue?

Edit: removing the reference did not fix the problem. It seems something may be messed up with the IIS configuration?

Josh M.
  • 26,437
  • 24
  • 119
  • 200
  • I'm also using dataannotationsextensions and am not having any issues. The only difference I see is that you have the "AdditionalMetadata" attribute in use which I've never used. Try removing it and see if it still errors? – CD Smith Mar 17 '12 at 01:38
  • No luck. This code all worked fine before (I haven't changed this model). Works fine locally, just not when deployed to the server. – Josh M. Mar 17 '12 at 01:57
  • 1
    I found this link, it may shed some light. http://fluentvalidation.codeplex.com/workitem/7072 – CD Smith Mar 17 '12 at 02:03
  • Hmmm. I did try the suggestion to no avail. But I'm not using the Fluent library. – Josh M. Mar 17 '12 at 02:50
  • 5
    Removing `` and `` from the `appSettings` in the root `Web.config` fixed the problem. – Josh M. Mar 17 '12 at 16:49
  • 4
    I saw someone else say that fixed the problem too but you lose client side validation by doing that. If you can live without it then it's not a problem. But it would be better for performance to not have to post the model to the server just to run model validation and return back to the client if it fails. – CD Smith Mar 17 '12 at 18:10
  • This is a weird case for me. I have the root `Web.config` which points to another config file, `\Configuration\[Region]\AppSettings.config`. I removed the aforementioned settings from this file. They do, however still exist in the `\Views\Web.config` file. However, it does appear that JS validation is not currently working. Which I DO want to work so I guess I have some more digging to do! – Josh M. Mar 17 '12 at 22:24
  • Show Your View too.please. – Amirhossein Mehrvarzi Jul 23 '13 at 03:26
  • 2
    Late to this (to say the least), but... In your controller, could you do a watch/trace of the contents of `ModelValidatorProviders.Providers`? `HTMLHelper` (which is what throws this error) only gets unobtrusive validation rules by consulting those providers, and if `DataAnnotationExtensions` isn't causing it (and we'll assume the default `DataAnnotationModelValidatorProvider` isn't causing it by itself either), there should be another provider inthere, for this error to occur. – JimmiTh Aug 06 '13 at 15:46
  • What version is your local IIS and what version is the server's? – mkorman Aug 27 '13 at 16:48
  • are you using FluentValidation or some DI? – Kristof Degrave Nov 06 '13 at 13:40
  • hmm, was getting the same error...also removed those two things from the web.config and fixed it. I have no clue. – Bohn Dec 23 '14 at 23:03

5 Answers5

20

JimmiTh's comment on the question provided a key insight for me to resolve this for myself.

In my case, I definitely did add an additional provider to ModelValidatorProviders. I added a custom validation factory (using Fluent Validation) with this code in my Global.asax.cs file:

ModelValidatorProviders.Providers.Add(
    new FluentValidationModelValidatorProvider(validatorFactory));

But using multiple providers isn't necessarily problematic. What seems to be problematic is if multiple providers provide the same validators, because that will register the same rules multiple times, causing the mentioned problem with the Microsoft unobtrusive validation code.

I ended up removing the following line from the same file as I decided I didn't need to use both providers:

FluentValidationModelValidatorProvider.Configure();

The Configure method above is itself adding a provider to ModelValidatorProviders, and I was effectively registering the same validator class twice, hence the error about non-unique "validation type names".

The SO question jquery - Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique points to another way that using multiple providers can lead to the mentioned problem. Each provider can be configured to add an 'implicit required attribute to 'value types' (i.e. view model properties that aren't nullable). To resolve this particular issue, I could change my code to the following so that none of the providers add implicit required attributes:

FluentValidationModelValidatorProvider.Configure(
    provider => provider.AddImplicitRequiredValidator = false);


DependencyResolverValidatorFactory validatorFactory =
    new DependencyResolverValidatorFactory();

FluentValidationModelValidatorProvider validatorFactoryProvider =
    new FluentValidationModelValidatorProvider(validatorFactory);

validatorFactoryProvider.AddImplicitRequiredValidator = false;
ModelValidatorProviders.Providers.Add(validatorFactoryProvider);


DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 
Community
  • 1
  • 1
Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
14

My case was that I had

.NotEmpty()

and

.NotNull()

at the same time, only one is needed.

PmanAce
  • 4,000
  • 2
  • 24
  • 29
5

If you are using FluentValidation side by side with DataAnnotations this can happen.

When FluentValidation is in action you may need to remove DataAnnotationsModelValidatorProvider from the registered ModelValidatorProviders in Application_Start method.

FluentValidationModelValidatorProvider.Configure(); 
// Remove data annotations validation provider 
ModelValidatorProviders.Providers.Remove(
            ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().First());
Veysel Ozdemir
  • 675
  • 7
  • 12
  • 1
    Saved me from wasting a load more time...thanks this exactly fixes the issue raised in the way you described – David Ward Oct 14 '19 at 12:25
0

Please, update the web.config file :

<configuration>
    <appSettings>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
</configuration>
Community
  • 1
  • 1
Vikram Jain
  • 5,498
  • 1
  • 19
  • 31
0

In my case I had added both NotEmpty and Length conditions to the validation rules at the same time.

RuleFor(x => x.Code).NotEmpty().Length(1, 10);

When I removed the NotEmpty condition the error disappeared.

basic
  • 11,673
  • 2
  • 9
  • 26