Fluent Validations. Error: Validation type names in unobtrusive client validation rules must be unique
Asked Answered
O

2

15

I got the erorr:

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

I used server validation. And all worked fine. But now I`m stating to use client-side validation and I got this problem.

This is my validation class code:

public class TestViewDataValidation : BaseTestCreateViewDataValidation<BaseTestCreateViewData>
    {

public TestViewDataValidation ()
        {
            this.RuleFor(x => x.Login).NotNull();
            this.RuleFor(x => x.Login).NotEmpty();
            this.RuleFor(x => x.Login).EmailAddress();          
        }
}

But if I leave one validator - all works fine. What should I do to have more that one validation for field.

Outthink answered 22/1, 2011 at 11:8 Comment(0)
W
17

FluentValidation.NET is called Fluent because it provides a fluent interface for chaining methods:

public TestViewDataValidation()
{
    RuleFor(x => x.Login)
        .NotNull()
        .NotEmpty()
        .EmailAddress();
}

Remark: the usage of NotNull and NotEmpty rules seem reduntant to me in this case. NotEmpty should be enough.

Weariless answered 22/1, 2011 at 14:14 Comment(0)
B
28

This error is shown if you have the same validation on the same element more than once.

Not setting AddImplicitRequiredAttributeForValueTypes = false for both the default DataAnnontations and your FluentValidation will add a Required validation on any ValueTypes (like an int). If you at the same time add a RuleFor (or a [Required] attribute) on any ValueType you will have an extra Required for that field.

For that reason (I want to set all validations explicitly) I have the following in my Application_Start():

var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new AttributedValidatorFactory());
ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;
Berg answered 5/4, 2011 at 13:1 Comment(2)
Like Darin is saying, adding NotNull and NotEmpty is redundant, and not only that. It enforces the same validation twice, hence the error i described here in my post...Sky
somehow, this don't work for clientside validation only for serverside..on client side asp.net mvc emit required attributes on all fields no matter that I set AddImplicitRequiredAttributeForValueTypes to false :( I ask similira to this on this post #8012115 and didn't get answer...am I doing something wrong or validation rules and logic are inconstant on server and on client?? thanxRomona
W
17

FluentValidation.NET is called Fluent because it provides a fluent interface for chaining methods:

public TestViewDataValidation()
{
    RuleFor(x => x.Login)
        .NotNull()
        .NotEmpty()
        .EmailAddress();
}

Remark: the usage of NotNull and NotEmpty rules seem reduntant to me in this case. NotEmpty should be enough.

Weariless answered 22/1, 2011 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.