I have a joda datetime
attribute in my request object and I have used @DateTimeFormat
for the date formatting.
But when I enter an invalid dat
The approach you linked to in your question should work. But the message key you should use is for instance typeMismatch.org.joda.time.DateTime
.
Even though you are not manually rejecting the value anywhere Spring will automatically know where to look for the message based on the rules described in the JavaDoc of DefaultMessageCodesResolver.
In the situation you describe Spring will look for following codes specifically:
typeMismatch.request.myDate
- Conversion message for attribute named "myDate" on an object named "request"typeMismatch.myDate
- Conversion message for attribute named myDatetypeMismatch.org.joda.time.DateTime
- Conversion message for DateTime typetypeMismatch
- General conversion error messageSo you can define some general typeMismatch
message like Incorrect format and then define more specific messages as needed. The more specific error codes have higher priority than those below it. So if you have both typeMismatch
and typeMismatch.org.joda.time.DateTime
messages defined, the latter will be used.