验证消息包含“{PropertyName}”而不是财产名称

发布于 2024-10-15 02:39:48 字数 859 浏览 8 评论 0原文

我使用流畅的验证和客户端不引人注目的验证。

 <fieldset class="edit-root-form">
    <p>
        @Html.LabelFor(model => model.Login, UserRes.Login)
        @Html.TextBoxFor(model => model.Login)
        @Html.ValidationMessageFor(model => model.Login)
    </p>         
</fieldset>

流畅的验证规则:

 this.RuleFor(x => x.Login).NotNull().EmailAddress()

我收到这样的错误消息:'{PropertyName}'不能为空。

生成的 html:

<input data-val="true" data-val-regex="&amp;#39;{PropertyName}&amp;#39; is not a valid
email address."  data-val-required="&amp;#39;{PropertyName}&amp;#39; must not be
 empty." id="Login" name="Login" type="text" value="" class="input-validation-error">

为什么 MVC 不替换 PropertyName 真实字段名称?

I use fluent validation with client side unobtrusive validation.

 <fieldset class="edit-root-form">
    <p>
        @Html.LabelFor(model => model.Login, UserRes.Login)
        @Html.TextBoxFor(model => model.Login)
        @Html.ValidationMessageFor(model => model.Login)
    </p>         
</fieldset>

Fluent validation rule:

 this.RuleFor(x => x.Login).NotNull().EmailAddress()

And I got error message like this: '{PropertyName}' must not be empty.

Genered html:

<input data-val="true" data-val-regex="&#39;{PropertyName}&#39; is not a valid
email address."  data-val-required="&#39;{PropertyName}&#39; must not be
 empty." id="Login" name="Login" type="text" value="" class="input-validation-error">

Why MVC don`t replace PropertyName real field name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-10-22 02:39:48

对我来说效果很好。我正在使用最新的 FluentValidation 版本 (2.0.0.0) 和 ASP.NET MVC 3 RTM。

模型和验证器:

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

public class MyViewModelValidator : AbstractValidator<MyViewModel>
{
    public MyViewModelValidator()
    {
        RuleFor(x => x.Login).NotNull().EmailAddress();
    }
}

控制器:

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

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

视图:

@model AppName.Models.MyViewModel
<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.LabelFor(model => model.Login, "Login")
    @Html.TextBoxFor(model => model.Login)
    @Html.ValidationMessageFor(model => model.Login)
    <input type="submit" value="OK" />
}

Global.asax中的Application_Start

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
}

两种情况:

  1. 将文本框留空:'Login'不能为空。 显示验证错误消息。
  2. 输入无效的电子邮件地址:“登录”不是有效的电子邮件地址。将显示验证错误消息。

最后,这是为文本框生成的 HTML:

<input data-val="true" data-val-regex="&#39;Login&#39; is not a valid email address." data-val-regex-pattern="^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$" data-val-required="&#39;Login&#39; must not be empty." id="Login" name="Login" type="text" value="" />

Works fine for me. I am using the latest FluentValidation version (2.0.0.0) and ASP.NET MVC 3 RTM.

Model and validator:

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

public class MyViewModelValidator : AbstractValidator<MyViewModel>
{
    public MyViewModelValidator()
    {
        RuleFor(x => x.Login).NotNull().EmailAddress();
    }
}

Controller:

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

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

View:

@model AppName.Models.MyViewModel
<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.LabelFor(model => model.Login, "Login")
    @Html.TextBoxFor(model => model.Login)
    @Html.ValidationMessageFor(model => model.Login)
    <input type="submit" value="OK" />
}

Application_Start in Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory()));
}

Two cases:

  1. Leave the textbox empty: 'Login' must not be empty. validation error message is shown.
  2. Type an invalid email: 'Login' is not a valid email address. validation error message is shown.

And finally here's the generated HTML for the textbox:

<input data-val="true" data-val-regex="&#39;Login&#39; is not a valid email address." data-val-regex-pattern="^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$" data-val-required="&#39;Login&#39; must not be empty." id="Login" name="Login" type="text" value="" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文