C
cmilley2000
Dear Spring MVC experts!
I am using a controller of type AbstractWizardFormController to
implement a multi-page form entry workflow. This controller operates
on a session-scoped command object which has a property of type
java.util.Date. On page 3 of the wizard, the user enters a date into a
textbox which will be bound to a java.util.Date property of the command
object.
In my controller, I am registering a custom editor as follows:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
The data binding process is behaving as I would expect. If the user
types an invalid date into the textbox, then the field cannot be bound
and the framework adds a FieldError to the Errors object with code
"typeMismatch". Unfortunately, this FieldError results in a very
un-user-friendly error message being put on the screen. The text of
the message is:
"Failed to convert property value of type [java.lang.String] to
required type [java.util.Date] for property billDate; nested exception
is java.lang.IllegalArgumentException: Could not parse date:
Unparseable date: "xyz" "
It is not at all clear to me how to transform this ugly error message
to a user-friendly message. I can think of a number of possible ways
of doing this, but all seem overly-complex. Is there a standard way to
map data-binding errors to user-friendly messages? Thanks in advance
for your help.
Regards,
Craig
I am using a controller of type AbstractWizardFormController to
implement a multi-page form entry workflow. This controller operates
on a session-scoped command object which has a property of type
java.util.Date. On page 3 of the wizard, the user enters a date into a
textbox which will be bound to a java.util.Date property of the command
object.
In my controller, I am registering a custom editor as follows:
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
CustomDateEditor editor = new CustomDateEditor(df, false);
binder.registerCustomEditor(Date.class, editor);
}
The data binding process is behaving as I would expect. If the user
types an invalid date into the textbox, then the field cannot be bound
and the framework adds a FieldError to the Errors object with code
"typeMismatch". Unfortunately, this FieldError results in a very
un-user-friendly error message being put on the screen. The text of
the message is:
"Failed to convert property value of type [java.lang.String] to
required type [java.util.Date] for property billDate; nested exception
is java.lang.IllegalArgumentException: Could not parse date:
Unparseable date: "xyz" "
It is not at all clear to me how to transform this ugly error message
to a user-friendly message. I can think of a number of possible ways
of doing this, but all seem overly-complex. Is there a standard way to
map data-binding errors to user-friendly messages? Thanks in advance
for your help.
Regards,
Craig