Validation type names in unobtrusive client validation rules must be unique
Asked Answered
P

5

39

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?

Philologian answered 17/3, 2012 at 0:34 Comment(12)
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?Formosa
No luck. This code all worked fine before (I haven't changed this model). Works fine locally, just not when deployed to the server.Philologian
I found this link, it may shed some light. fluentvalidation.codeplex.com/workitem/7072Formosa
Hmmm. I did try the suggestion to no avail. But I'm not using the Fluent library.Philologian
Removing <add key="ClientValidationEnabled" value="true"/> and <add key="UnobtrusiveJavaScriptEnabled" value="true"/> from the appSettings in the root Web.config fixed the problem.Philologian
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.Formosa
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!Philologian
Show Your View too.please.Laskowski
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.Bookrack
What version is your local IIS and what version is the server's?Arbil
are you using FluentValidation or some DI?Gustavogustavus
hmm, was getting the same error...also removed those two things from the web.config and fixed it. I have no clue.Arletha
U
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; 
Unasked answered 28/3, 2014 at 17:33 Comment(0)
B
14

My case was that I had

.NotEmpty()

and

.NotNull()

at the same time, only one is needed.

Bakerman answered 22/6, 2016 at 20:38 Comment(0)
S
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());
Spread answered 21/9, 2016 at 19:23 Comment(1)
Saved me from wasting a load more time...thanks this exactly fixes the issue raised in the way you describedHeadboard
W
0

Please, update the web.config file :

<configuration>
    <appSettings>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
</configuration>
Wack answered 8/11, 2013 at 4:48 Comment(0)
S
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.

Schweinfurt answered 7/12, 2019 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.