【发布时间】:2016-09-29 19:55:23
【问题描述】:
我正在使用 Bean Validation API 和我的自定义消息进行表单验证:
@NotNull(message = "Please enter your birthday.")
@DateTimeFormat(pattern = "MM/dd/yyyy")
@Past(message = "That's impossible.")
private Date birthday;
在我输入错误的日期格式之前一切正常。然后我在我的页面上收到了如此冗长而冗长的错误消息:
Failed to convert property value of type [java.lang.String] to required type
[java.util.Date] for property birthday; nested exception is
bla-bla-bla...
由于org.springframework.format.annotation.DateTimeFormat 中的@interface DateTimeFormat 中没有字段message,因此我无法像使用其他注释那样在其中添加消息。
如何在此处指定"Please use MM/dd/yyyy format" 之类的自定义消息?
【问题讨论】:
-
请注意,您看到的是转换,而不是验证错误。只有在使用从 String 转换的值初始化 bean 后,验证才会启动。
标签: java spring validation