开发者

Fluent Validation not working on length

开发者 https://www.devze.com 2023-02-05 15:29 出处:网络
I am trying to get Fluent Validation to work correctly on my client side validation.I am using ASP.NET MVC 3.

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());


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消