有沒有人設法讓自定義IMessageInterpolator工作到 啓用錯誤消息的自定義。我試過在此網頁上按照 指示操作,但無濟於事。 http://codelog.climens.net/2009/03/04/nhibernate-validator-custom-messages/nHibernate驗證器自定義IMessageInterpolator
尋找代碼DefaultMessageInterpolator似乎很漂亮 烘烤到框架,所以有什麼我失蹤。
我已經包括我的單元測試,以顯示我如何實現它。
namespace TestValidator
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestValidator()
{
var customer = new Customer();
ClassValidator classValidator = new ClassValidator(customer.GetType());
InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);
Assert.IsTrue(validationMessages.Length == 1);
Assert.IsTrue(validationMessages[0].Message == "may not be null or empty");
}
[TestMethod]
public void TestCustomInterpolator()
{
ValidatorEngine ve = new ValidatorEngine();
NHVConfiguration nhvc = new NHVConfiguration();
nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(CustomMessageInterpolator).AssemblyQualifiedName;
ve.Configure(nhvc);
var customer = new Customer();
ClassValidator classValidator = new ClassValidator(customer.GetType());
InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);
Assert.IsTrue(validationMessages.Length == 1);
// THIS TEST FAILS!!
Assert.IsTrue(validationMessages[0].Message == "CustomMessageInterpolator");
}
}
public class Customer
{
public virtual int CustomerId { get; set; }
[NotNullNotEmpty]
public string CustomerName { get; set; }
}
public class CustomMessageInterpolator : IMessageInterpolator
{
public CustomMessageInterpolator()
{
}
public string Interpolate(string message, IValidator validator, IMessageInterpolator defaultInterpolator)
{
return "CustomMessageInterpolator";
}
}
}
你可以添加你的NHibernate和NHibernate.Validator配置嗎? – zihotki 2009-06-03 23:03:22