Java Spring MVC 表单验证日期字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24053139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC form validation Date field
提问by user1459144
I have a form field that should be converted to a Date object, as follows:
我有一个应转换为 Date 对象的表单字段,如下所示:
<form:input path="date" />
And I have requirement to make validation in following way:
我需要以下列方式进行验证:
If user leaves this field es empty. I need to set some default date.
If User enetered in unacceptable format, I need to present an error.
如果用户将此字段保留为空。我需要设置一些默认日期。
如果用户以不可接受的格式输入,我需要显示错误。
The problem is that I can meet either 1st or 2nd requirement.
问题是我可以满足第一个或第二个要求。
- I can register PropertyEditorand in case date is unacceptable set null, and in case this field null set some default date. But with this approach I can't meet 2nd requirement because if I convert it to null I won't have ability to register 2nd error to user.
- I can set @DateTimeFormat(pattern="dd-MMM-yyyy")annotation and provide appropriate typeMismatch error but it still return this error when user leaves empty value in this field.
- 我可以注册PropertyEditor,如果日期不可接受,则设置为 null,如果此字段为 null,则设置一些默认日期。但是使用这种方法我无法满足第二个要求,因为如果我将其转换为 null,我将无法向用户注册第二个错误。
- 我可以设置 @DateTimeFormat(pattern="dd-MMM-yyyy")注释并提供适当的 typeMismatch 错误,但是当用户在此字段中留下空值时它仍然返回此错误。
Is any good solution to such problem ?
这样的问题有什么好的解决方案吗?
采纳答案by d3day
There is a way to do this if you'll move from Spring MVC validators to Hibernate. It's not a big deal because Hibernate validators work well with Spring Validator interface (also you may be interested in SmartValidator intreface which supports groups).
如果您从 Spring MVC 验证器转移到 Hibernate,有一种方法可以做到这一点。这没什么大不了的,因为 Hibernate 验证器可以很好地与 Spring Validator 接口配合使用(您也可能对支持组的 SmartValidator 接口感兴趣)。
After that you can specify@NotNull
annotation on the date field.
之后,您可以@NotNull
在日期字段上指定注释。
In properties file just addNotNull.[formName].[dateField]
=This message will be displayed if no date sent
typeMismatch.[formName].[dateField]
=This message will be displayed if spring was not able to convert input to date object (format validation error)
在属性文件中只需添加NotNull.[formName].[dateField]
= 如果没有发送日期,
typeMismatch.[formName].[dateField]
将显示此消息 = 如果 spring 无法将输入转换为日期对象(格式验证错误),将显示此消息
For format check you can use CustomDateEditor
.
对于格式检查,您可以使用CustomDateEditor
.
With this solution you will get expected validation error messages in each case you've specified.
使用此解决方案,您将在指定的每种情况下获得预期的验证错误消息。
回答by Serge Ballesta
When I have to face such a problem, I reverse the logic. I do not set a default value to an empty field, but I pre-load the field with the default value in the render part of the operation. So I can safely valid with just @DateTimeFormat(pattern="dd-MMM-yyyy")
because an empty field suppose that the user indendly removed the default value which is an error.
当我不得不面对这样的问题时,我将逻辑颠倒过来。我没有将默认值设置为空字段,但我在操作的渲染部分使用默认值预加载了该字段。所以我可以安全地仅@DateTimeFormat(pattern="dd-MMM-yyyy")
因为一个空字段假设用户独立地删除了默认值,这是一个错误。
So IMHO you should set your default values in the GET part of you controller for that form and stick to a simple @DateTimeFormat(pattern="dd-MMM-yyyy")
annotation.
所以恕我直言,您应该在控制器的 GET 部分中为该表单设置默认值并坚持使用简单的@DateTimeFormat(pattern="dd-MMM-yyyy")
注释。
I have an application using a @ModelAttribute
annotated object containing a date field.
In the object class, the date field is annotated on getter and setter with DateTimeFormat(pattern="dd/MM/yyyy")
. And Spring (3.2.4) accept an empty value for the date field which simply sets it to null. So you should use @DateTimeFormat(pattern="dd-MMM-yyyy")
and still be able to accept null values.
我有一个使用@ModelAttribute
包含日期字段的带注释对象的应用程序。在对象类中,日期字段在 getter 和 setter 上使用DateTimeFormat(pattern="dd/MM/yyyy")
. 并且 Spring (3.2.4) 接受日期字段的空值,它只是将其设置为 null。所以你应该使用@DateTimeFormat(pattern="dd-MMM-yyyy")
并且仍然能够接受空值。