Question

i followed all of these steps in this tutorial:

Created a validator class

public class ProjectValidator : AbstractValidator<ProjectViewModel>
{
    public ProjectValidator()
    {
        //RuleFor(h => h.Milestone).NotEmpty().WithName("Milestone");
        RuleFor(h => h.Applications).NotNull().WithName("Application");
        RuleFor(h => h.InitiativeName).NotNull().WithName("Business Aligned Priority");
        RuleFor(h => h.BusinessDriverId).NotNull().WithName("Business Driver");
        RuleFor(h => h.FundingTypeId).NotNull().WithName("Funding Type");
        RuleFor(h => h.Description).NotEmpty().WithName("Description");
        RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
        RuleFor(h => h.Sponsors).NotNull().WithName("Sponsors");
    }
}

Put an attribute on my DTO to specific this validtor

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

but after a form post when i go to check the ModelState errors list, the errors i see are coming from the asp.net-mvc default validation.

 public ActionResult UpdateMe(ProjectViewModel entity)
    {
        Project existingProject = this.Repository.Fetch<Project>(entity.Id);

        UpdateProperties(entity, existingProject);
        var allErrors = ModelState.Values.SelectMany(v => v.Errors);
        if (allErrors.Count() > 0)
        {

any suggestions on why its not picking up the fluent. validator ?? I have added an image below of what i see on the gui

enter image description here

if i call the validator directly in code it works just fine:

 ProjectValidator validator = new ProjectValidator();
 ValidationResult result = validator.Validate(entity);
Was it helpful?

Solution

I'm not sure what type of HTML element FundingTypeId is but I'm assuming it is a dropdown list. If nothing is selected then it will give you this error. This is unfortunately one of the limitations of the FV integration with MVC which is caused by the bad design of MVC's default model binder. That message is not generated by FV but rather is produced by the DefaultModelBinder, in this case where the incoming value cannot be converted to the property type.

Check out these 2 questions which I posted on the Fluent Validation discussion forum: http://fluentvalidation.codeplex.com/discussions/250798 http://fluentvalidation.codeplex.com/discussions/253389

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top