Spring MVC Form Validation Date Field
I have a form field that needs to be converted to a Date object, as follows:
<form:input path="date" />
And I have a requirement to do validation as follows:
If the user leaves this field blank. I need to set a default date.
If the user participated in an unacceptable format, I need to report an error.
The problem is that I can satisfy either the 1st or 2nd requirement.
- I can register a PropertyEditor , and in case the date is invalid, set it to null, and if this null field set some date by default. But with this approach, I can’t fulfill the second requirement, because if I convert it to null, I will not be able to register the 2nd error for the user.
- I can set the annotation @DateTimeFormat (pattern = "dd-MMM-yyyy") and provide a corresponding error like Mismatch, but it still returns this error when the user leaves an empty value in this field.
Is there any good solution to such a problem?