Search code examples
c#fluentvalidation

Fluent Validation validator results in an error before validation code is even added to it


I'm trying out Fluent Validation using the Contoso University project.

So I've added a validator attribute to an existing class:

[Validator(typeof(PersonValidator))]
public abstract class Person
{
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
}

My PersonValidator doesn't do anything yet:

public class PersonValidator : AbstractValidator<Person>
{
    public PersonValidator()
    {
    }
}

But when I access the create page for a Student my debugger stops on the EditorFor line....

 @Html.EditorFor(model => model.LastName, 
      new { htmlAttributes = new { @class = "form-control" } })

….and I get an error:

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

I don't appear to have the same validation on the same element more than once, so why am I getting the error? Can Fluent Validation work alongside MVC's built in validation?


Solution

  • I'm not sure why it would add an implicit required validator to a string field, but the problem went away when I configured the Fluent Validation Provider to not add implicit required validators in my Global.asax.cs file:

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

    I didn't want to change the behaviour of any existing data annotations so that is all I added