如何获得NHibernate.Validator插值消息 [英] How to get interpolated message in NHibernate.Validator

查看:240
本文介绍了如何获得NHibernate.Validator插值消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用NHibernate.Validator ASP.NET MVC客户端验证整合,而且我发现唯一的问题是,我根本无法非插值消息转换为人类可读的。我认为这将是一件容易的事,但竟然是在客户端验证中最难的部分。主要的问题是,因为它不是服务器端,我实际上只需要一个正在使用的验证属性,我实际上并不手头实例或其他任何东西。

I'm trying to integrate NHibernate.Validator with ASP.NET MVC client side validations, and the only problem I found is that I simply can't convert the non-interpolated message to a human-readable one. I thought this would be an easy task, but turned out to be the hardest part of the client-side validation. The main problem is that because it's not server-side, I actually only need the validation attributes that are being used, and I don't actually have an instance or anything else at hand.

下面是从我已经已经开始尝试一些摘录:

Here are some excerpts from what I've been already trying:

// Get the the default Message Interpolator from the Engine
IMessageInterpolator interp = _engine.Interpolator;
if (interp == null)
{
  // It is null?? Oh, try to create a new one
  interp = new NHibernate.Validator.Interpolator.DefaultMessageInterpolator();
}

// We need an instance of the object that needs to be validated, se we have to create one
object instance = Activator.CreateInstance(Metadata.ContainerType);

// we enumerate all attributes of the property. For example we have found a PatternAttribute
var a = attr as PatternAttribute;

// it seems that the default message interpolator doesn't work, unless initialized
if (interp is NHibernate.Validator.Interpolator.DefaultMessageInterpolator)
{
  (interp as NHibernate.Validator.Interpolator.DefaultMessageInterpolator).Initialize(a);
}

// but even after it is initialized the following will throw a NullReferenceException, although all of the parameters are specified, and they are not null (except for the properties of the instance, which are all null, but this can't be changed)
var message = interp.Interpolate(new InterpolationInfo(Metadata.ContainerType, instance, PropertyName, a, interp, a.Message));

我知道上面是一个相当复杂的code为一个看似简单的问题,但我仍然坚持无解。有没有什么办法让插值串出NHValidator的?

I know that the above is a fairly complex code for a seemingly simple question, but I'm still stuck without solution. Is there any way to get the interpolated string out of NHValidator?

推荐答案

好了,我知道这是一个老问题,但我碰到这个偶然试图做同样的事情时,它帮助我上手 - 所以我以为我会提供一个答案。

Ok, so I know this is an old question, but I stumbled across this when trying to do the same thing, and it helped me get started - so I thought I would provide an answer.

我觉得在这个问题的code是正确的轨道上,但有几个问题。内插器没有完全与的ResourceManager 文化详细信息初始化,它似乎并没有允许事实你只能有一个 DefaultMessageInterpolator 每验证属性。此外,您不需要您要验证得到插值消息对象的实例。

I think the code in the question was on the right track but there are a couple of problems. The interpolator was not completely initialised with the ResourceManager and Culture details, and it doesn't seem to allow for the fact that you can only have one DefaultMessageInterpolator per validation attribute. Also, you don't need an instance of the object you are validating to get an interpolated message.

在在的问题,你在哪里的属性值初始化插补器的code,你还需要使用的ResourceManager 来的细节,以初始化插使用

In the code in the question, where you are initialising the interpolator with the attribute value, you also need to initialise the interpolator with details of the ResourceManager to be used.

这可以使用重载初始化方法来实现对 DefaultMessageInterpolator 具有以下签名:

This can be done using the overloaded Initialize method on DefaultMessageInterpolator which has the following signature:

    public void Initialize(ResourceManager messageBundle, 
                           ResourceManager defaultMessageBundle, 
                           CultureInfo culture)

第一个参数是您要使用的错误消息您自己的资源文件情况下,用户定义的ResourceManager,你可以传递一个空,如果你只是想使用默认ResouceManager,第二个参数是默认的ResourceManager - 你可以通过

The first parameter is a user-defined ResourceManager in case you want to use your own resource file for error messages, you can pass a null if you just want to use the default ResouceManager, the second parameter is the default ResourceManager - you can pass

  new ResourceManager( 
      NHibernate.Validator.Cfg.Environment.BaseNameOfMessageResource,
      Assembly.GetExecutingAssembly());

对于这一点,最后一个参数是用文化,(NHibernate.Validator自带的资源文件具有多种语言验证消息) - 如果你传递一个空这个它将只使用的CultureInfo .CurrentCulture

最后,你只能有一个 DefaultMessageInterpolator 每个属性,所以你需要创建一个新的 DefaultMessageInterpolator 的每个验证属性。你可以使用的 DefaultMessageInterpolatorAggregator 来处理这个问题,或者只是推出自己。

Lastly, you can only have one DefaultMessageInterpolator per attribute, so you will need to create a new DefaultMessageInterpolator for each validation attribute. You could make use of the DefaultMessageInterpolatorAggregator to handle this, or just roll your own.

我希望这可以帮助别人。

I hope this helps someone.

这篇关于如何获得NHibernate.Validator插值消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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