我想將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中獲取內插字符串?