Spring MVC form validation Date field

后端 未结 2 1846
自闭症患者
自闭症患者 2021-01-12 11:18

I have a form field that should be converted to a Date object, as follows:


And I have requirement to mak

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 11:51

    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).

    After that you can specify
    @NotNull
    annotation on the date field.

    In properties file just add
    NotNull.[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)

    For format check you can use CustomDateEditor.

    With this solution you will get expected validation error messages in each case you've specified.

提交回复
热议问题