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

前端 未结 2 1158
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 05:04

I got the erorr:

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

相关标签:
2条回答
  • 2020-12-09 05:30

    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.

    0 讨论(0)
  • 2020-12-09 05:40

    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;
    
    0 讨论(0)
提交回复
热议问题