0

i want to override the default asp.net-mvc validation when posting a form so i tried using fluent.validation

i created a validator class (ProjectValidator)

public class ProjectValidator : AbstractValidator<Project>
{
    public ProjectValidator()
    {
        RuleFor(h => h.Application).NotNull().WithName("Application");
        RuleFor(h => h.FundingType).NotNull().WithName("Funding Type");
        RuleFor(h => h.Description).NotEmpty().WithName("Description");
        RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
    }
}

i put an attribute on my Data Transfer Object class

[Validator(typeof(ProjectValidator))]
  public class ProjectViewModel
  {
      ...
  }

and i put this in application_start();

 DataAnnotationsModelValidatorProvider
            .AddImplicitRequiredAttributeForValueTypes = false;

 ModelValidatorProviders.Providers.Add(
            new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));

but when i post a form that uses this object, i get the following error:

Method not found: 'System.Collections.Generic.IEnumerable`1 FluentValidation.IValidatorDescriptor.GetValidatorsForMember(System.String)'.

any suggestions?

1 Answer 1

2

It could be a problem related to the assembly versions you are using. Here are the steps that work for me with the latest version of FluentValidation.NET and ASP.NET MVC 3:

  1. Create a new ASP.NET MVC 3 project using the default Visual Studio template.
  2. Install the FluentValidation.MVC3 NuGet package.
  3. Add the view model and its corresponding validator (notice that in your case you have the validator for type Project - AbstractValidator<Project> whereas your view model is called ProjectViewModel which is inconsistent. The validator must be associated to the view model):

    public class ProjectValidator : AbstractValidator<ProjectViewModel>
    {
        public ProjectValidator()
        {
            RuleFor(h => h.Application).NotNull().WithName("Application");
            RuleFor(h => h.FundingType).NotNull().WithName("Funding Type");
            RuleFor(h => h.Description).NotEmpty().WithName("Description");
            RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
        }
    }
    
    [Validator(typeof(ProjectValidator))]
    public class ProjectViewModel
    {
        public string Application { get; set; }
        public string FundingType { get; set; }
        public string Description { get; set; }
        public string Name { get; set; }
    }
    
  4. In Application_Start register the validator:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    
        DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
        ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
    }
    
  5. Define a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new ProjectViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(ProjectViewModel model)
        {
            return View(model);
        }
    }
    
  6. And a view:

    @model Appame.Models.ProjectViewModel
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.Application)
            @Html.EditorFor(x => x.Application)
            @Html.ValidationMessageFor(x => x.Application)
        </div>
        <div>
            @Html.LabelFor(x => x.FundingType)
            @Html.EditorFor(x => x.FundingType)
            @Html.ValidationMessageFor(x => x.FundingType)
        </div>
        <div>
            @Html.LabelFor(x => x.Description)
            @Html.EditorFor(x => x.Description)
            @Html.ValidationMessageFor(x => x.Description)
        </div>
        <div>
            @Html.LabelFor(x => x.Name)
            @Html.EditorFor(x => x.Name)
            @Html.ValidationMessageFor(x => x.Name)
        </div>
    
        <input type="submit" value="OK" />
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.