Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Validation not working on length

I am trying to get Fluent Validation to work correctly on my client side validation. I am using ASP.NET MVC 3.

I have a title that is required and it must be between 1 and 100 characters long. So while I am typing in the title an error message displays that is not in my ruleset. Here is my rule set:

RuleFor(x => x.Title)
   .NotEmpty()
   .WithMessage("Title is required")
   .Length(1, 100)
   .WithMessage("Title must be less than or equal to 100 characters");

Here is the error message that is displayed:

Please enter a value less than or equal to 100

I'm not sure what I am doing wrong. Here is my global.asax:

// FluentValidation
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

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

ModelMetadataProviders.Current = new FluentValidationModelMetadataProvider(
   new AttributedValidatorFactory());
like image 752
Brendan Vogt Avatar asked Jan 17 '11 13:01

Brendan Vogt


1 Answers

Works fine for me. Here are the steps:

  1. Create a new ASP.NET MVC 3 RTM project using the default Visual Studio Template
  2. Download the latest FluentValidation.NET
  3. Reference the FluentValidation.dll and FluentValidation.Mvc.dll assemblies (be careful there are two folders inside the .zip: MVC2 and MVC3 so make sure to pick the proper assembly)

Add a model:

[Validator(typeof(MyViewModelValidator))]
public class MyViewModel
{
    public string Title { get; set; }
}

and a corresponding validator:

public class MyViewModelValidator : AbstractValidator<MyViewModel>
{
    public MyViewModelValidator()
    {
        RuleFor(x => x.Title)
           .NotEmpty()
           .WithMessage("Title is required")
           .Length(1, 5)
           .WithMessage("Title must be less than or equal to 5 characters");
    }
}

Add to Application_Start:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

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

ModelMetadataProviders.Current = new FluentValidationModelMetadataProvider(
    new AttributedValidatorFactory());

Add a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

and the corresponding view:

@model SomeApp.Models.MyViewModel
@{
    ViewBag.Title = "Home Page";
}
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.TextBoxFor(x => x.Title)
    @Html.ValidationMessageFor(x => x.Title)
    <input type="submit" value="OK" />
}

Now try to submit the form leaving the Title input empty => client side validation kicks in and the Title is required message is shown. Now start typing some text => the error message disappears. Once you type more than 5 characters in the input box the Title must be less than or equal to 5 characters validation message appears. So everything seems to work as expected.

like image 198
Darin Dimitrov Avatar answered Sep 18 '22 00:09

Darin Dimitrov