NHibernate.Validator を ASP.NET MVC クライアント側の検証と統合しようとしていますが、唯一の問題は、補間されていないメッセージを人間が読める形式に変換できないことです。これは簡単な作業だと思っていましたが、クライアント側の検証で最も難しい部分であることが判明しました。主な問題は、サーバー側ではないため、実際には使用されている検証属性のみが必要であり、実際にはインスタンスやその他のものを手元に持っていないことです。
以下は、私がすでに試したことからの抜粋です。
// 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 から取得する方法はありますか?