首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在@Email hibernate验证器中使用占位符

如何在@Email hibernate验证器中使用占位符
EN

Stack Overflow用户
提问于 2012-01-29 07:56:51
回答 1查看 2K关注 0票数 0

默认情况下,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。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-01-29 21:06:50

如果您使用Hibernate Validator作为Bean验证提供者,则可以使用版本4.2中引入的ValueFormatterMessageInterpolator。要做到这一点,只需在检索Validator时设置插值器,例如:

代码语言:javascript
复制
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中找到更多信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9049693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档