MVC 3、NHibernate 验证器和消息插值器

发布于 2024-10-20 19:24:12 字数 1319 浏览 2 评论 0原文

我已关注这篇文章并通过了测试显示当调用 Validator.IsValid(someEntity) 失败时从资源文件返回的自定义验证错误消息。

我试图将其转换为 MVC3 中新的不引人注目的客户端验证 这篇文章。这或多或少也有效 - 客户端验证确实如此,因此我可以假设我的 NHValidators 接线良好,正如您可以看到以下输出:

<input data-val="true" data-val-required="{NotNullNotEmpty}" id="Username" name="Username" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true">

我的问题是,这次 CustomMessageInterpolator 尚未触发 - 资源字符串已尚未翻译此属性(仍在大括号中):{NotNullNotEmpty}。

我的测试和网络都使用相同的 NHValidator 配置:

var cfg = new FluentConfiguration();
cfg
.SetMessageInterpolator<CustomMessageInterpolator>()
.SetCustomResourceManager("MyProject.Core.Resources.ValidationMessages", Assembly.Load("MyProject.Core"))
.SetDefaultValidatorMode(ValidatorMode.UseAttribute)
.Register(Assembly.Load("MyProject.Core").ValidationDefinitions())

为了更好的衡量,这里是模型片段:

public class LoginModel
  {
    [NotNullNotEmpty(Message = "{NotNullNotEmpty}")]
    public string Username { get; set; }

我知道,但任何想法都将不胜感激!

I have followed this article and have a passing test showing custom validation error messages being returned from a resource file when a call to Validator.IsValid(someEntity) fails.

I am trying to translate this to the new unobtrusive client-side validation in MVC3 describe by this article. This also more or less works - well the client-side validation does so I can assume my wiring of NHValidators is good, as you can see the following is output:

<input data-val="true" data-val-required="{NotNullNotEmpty}" id="Username" name="Username" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true">

My problem is that this time the CustomMessageInterpolator has not fired - the resource string has not been translated for this property (it's still in curly braces): {NotNullNotEmpty}.

Both my test and the web are usign the same NHValidator config:

var cfg = new FluentConfiguration();
cfg
.SetMessageInterpolator<CustomMessageInterpolator>()
.SetCustomResourceManager("MyProject.Core.Resources.ValidationMessages", Assembly.Load("MyProject.Core"))
.SetDefaultValidatorMode(ValidatorMode.UseAttribute)
.Register(Assembly.Load("MyProject.Core").ValidationDefinitions())

Just for good measure here's the model snippet:

public class LoginModel
  {
    [NotNullNotEmpty(Message = "{NotNullNotEmpty}")]
    public string Username { get; set; }

Long shot I know but any ideas would be appreciated!

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

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

发布评论

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

评论(2

音栖息无 2024-10-27 19:24:12

我在几个论坛上发布了它,但没有运气。

最后,我在视图模型中使用 DataAnnotations 来进行客户端验证。

当然,这确实是一个很糟糕的“解决方案”,因为我的代码中现在有两种类型的验证属性。 (如果想知道,是的,我宁愿保留我的 NH 验证属性 1. 因为我仍在从它们生成我的架构,2. 我喜欢 NH 在提交到数据库(即服务器端)之前进行验证的方式)。

如果有兴趣,请参阅如何使用资源文件来显示正确的标签你的控制。

此代码片段展示了如何设置自定义错误消息:

  [DisplayResource(typeof(LabelResources))]
  public class NewChildTagViewModel : ModuleViewModel
  {
    public int ParentId { get; set; }

    [Required]
    public string Title { get; set; }

    [Range(1, 1000, ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "Display_Order")]
    [Required]
    public int? DisplayOrder { get; set; }
  }

I had no luck with this having posted it around a few forums.

In the end I've resorted to using DataAnnotations instead in my View Models for client-side validation.

Of course this is really hacky and a horrible "solution" as I've now two types of validation attributes in my code. (if wondering, yes I'd rather keep my NH Validation attributes 1. as I'm still generating my schema from them and 2. I like the way NH validates before committing to the db i.e. server-side).

If interested, here's how to use resource files to show the correct labels for your controls.

This code snippet shows how to then also rig up custom error messages:

  [DisplayResource(typeof(LabelResources))]
  public class NewChildTagViewModel : ModuleViewModel
  {
    public int ParentId { get; set; }

    [Required]
    public string Title { get; set; }

    [Range(1, 1000, ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "Display_Order")]
    [Required]
    public int? DisplayOrder { get; set; }
  }
℉絮湮 2024-10-27 19:24:12

你能展示你的代码吗?我想您使用了某种自定义的 ModelValidatorProvider ?

我有一些东西像你所描述的那样工作。它需要按照此处描述的方式编写自定义 AssociatedValidatorProviderModelValidator

http://weblogs.asp.net/srkirkland/archive/2010/07/20/nhibernate -client-validator-asp-net-mvc-2-model-validation.aspx

但更改了上面文章中的 MessageOrDefault 方法以使用 NHibernate Validator DefaultMessageInterpolator 来转换消息。

听起来您已经完成了大部分工作,只需使用 DefaultMessageInterpolator 来翻译消息即可。

Can you show your code? I presume you used some sort of custom ModelValidatorProvider?

I've got something working like what you described. It required writing a custom AssociatedValidatorProvider and ModelValidator, along the lines described here

http://weblogs.asp.net/srkirkland/archive/2010/07/20/nhibernate-client-validator-asp-net-mvc-2-model-validation.aspx

but changing the MessageOrDefault method in the article above to use the NHibernate Validator DefaultMessageInterpolator to translate the messages.

It sounds like you got most of the way towards getting something working, and you just needed to use the DefaultMessageInterpolator to translate the messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文