MVC 3、NHibernate 验证器和消息插值器
我已关注这篇文章并通过了测试显示当调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在几个论坛上发布了它,但没有运气。
最后,我在视图模型中使用 DataAnnotations 来进行客户端验证。
当然,这确实是一个很糟糕的“解决方案”,因为我的代码中现在有两种类型的验证属性。 (如果想知道,是的,我宁愿保留我的 NH 验证属性 1. 因为我仍在从它们生成我的架构,2. 我喜欢 NH 在提交到数据库(即服务器端)之前进行验证的方式)。
如果有兴趣,请参阅如何使用资源文件来显示正确的标签你的控制。
此代码片段展示了如何设置自定义错误消息:
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:
你能展示你的代码吗?我想您使用了某种自定义的 ModelValidatorProvider ?
我有一些东西像你所描述的那样工作。它需要按照此处描述的方式编写自定义
AssociatedValidatorProvider
和ModelValidator
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
andModelValidator
, along the lines described herehttp://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.