默认情况下,org.hibernate.validator.constraints.Email会显示not a well-formed email address,但如果用户只需要输入格式良好的电子邮件,我就会显示foo is not a well-formed email address。所以我需要有一个foo的占位符。我的域对象中的属性用@Email注释。
我的资源包文件中有Email = {0} is not a well-formed email address,当用户在email字段中输入foo时,它会显示email is not a well-formed email address。
是否可以生成输入值并将其显示为错误消息?
我使用的是Hibernate 3.5和Spring MVC 3。
谢谢。
发布于 2012-01-29 21:06:50
如果您使用Hibernate Validator作为Bean验证提供者,则可以使用版本4.2中引入的ValueFormatterMessageInterpolator。要做到这一点,只需在检索Validator时设置插值器,例如:
Configuration<?> configuration = Validation.byDefaultProvider().configure();
Validator validator = configuration
.messageInterpolator(new ValueFormatterMessageInterpolator(
configuration.getDefaultMessageInterpolator()))
.buildValidatorFactory()
.getValidator();使用该内插器,您可以在错误消息{validatedValue} is not a well-formed email address中通过占位符{validatedValue}引用验证值。
您可以在HV reference guide中找到更多信息。
https://stackoverflow.com/questions/9049693
复制相似问题