我想将ASP.NET MVC客户端验证与NHibernate.Validator集成在一起,唯一的问题是我无法将非插值消息转换为一个人可读的。我认为这将是一件容易的事情,但事实证明这是客户端验证中最难的部分。主要的问题是,因为它不是服务器端,我实际上只需要正在使用的验证属性,而我实际上并没有实例或其他任何东西。如何在NHibernate.Validator中插入消息
下面是我一直在努力已经一些摘录:
// 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));
我知道上面是一个看似简单的问题相当复杂的代码,但我仍然坚持没有解决之道。有没有办法从NHValidator中获取内插字符串?