流利的验证不工作的长度 [英] Fluent Validation not working on length

查看:105
本文介绍了流利的验证不工作的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得流利的验证在我的客户端验证正常工作。我使用ASP.NET MVC 3。

我有需要的标题和它必须是1至100个字符之间。因此,虽然我的称号我打字显示一条错误消息,是不是在自己的规则。这里是我的规则集:

  RuleFor(X => x.Title)
   。不是空的()
   .WithMessage(标题是必需的)
   。长度(1,100)
   .WithMessage(标题必须小于或等于100个字符);

下面是显示错误消息:

 请输入小于或等于100的值

我不知道我做错了。这是我的Global.asax:

  // FluentValidation
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = FALSE;ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(
   新FluentValidationModelValidatorProvider(新AttributedValidatorFactory()));ModelMetadataProviders.Current =新FluentValidationModelMetadataProvider(
   新AttributedValidatorFactory());


解决方案

对于我来说工作正常。具体操作步骤如下:


  1. 使用默认创建一个新的ASP.NET MVC 3 RTM项目的Visual Studio模板

  2. 下载最新的 FluentValidation.NET

  3. 引用 FluentValidation.dll FluentValidation.Mvc.dll 组件(小心里面有两个文件夹.ZIP:MVC2和MVC3所以一定要挑选合适的装配)

添加一个模型:

  [验证(typeof运算(MyViewModelValidator))]
公共类MyViewModel
{
    公共字符串名称{搞定;组; }
}

和相应的验证:

 公共类MyViewModelValidator:AbstractValidator< MyViewModel>
{
    公共MyViewModelValidator()
    {
        RuleFor(X => x.Title)
           。不是空的()
           .WithMessage(标题是必需的)
           。长度(1,5)
           .WithMessage(标题必须小于或等于5个字符);
    }
}

添加到的Application_Start

  DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = FALSE;ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(
    新FluentValidationModelValidatorProvider(新AttributedValidatorFactory()));ModelMetadataProviders.Current =新FluentValidationModelMetadataProvider(
    新AttributedValidatorFactory());

添加控制器:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        返回查看(新MyViewModel());
    }    [HttpPost]
    公众的ActionResult指数(MyViewModel模型)
    {
        返回查看(模型);
    }
}

和相应的视图:

  @model SomeApp.Models.MyViewModel
@ {
    ViewBag.Title =主页;
}
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.js)TYPE =文/ JavaScript的>< / SCRIPT>
<脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.js)TYPE =文/ JavaScript的>< / SCRIPT>
@using(Html.BeginForm())
{
    @ Html.TextBoxFor(X => x.Title)
    @ Html.ValidationMessageFor(X => x.Title)
    <输入类型=提交VALUE =OK/>
}

现在尝试提交表单留下标题输入空=>在客户端验证踢和的是必需的标题消息的显示。现在开始键入一些文字=>错误信息消失。一旦你输入在输入超过5个字符框的标题必须小于或等于5个字符的出现验证消息。所以,一切似乎都如预期工作。

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.

这篇关于流利的验证不工作的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆