Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Validation type names in unobtrusive client validation rules must be unique

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...