|
From: <jue...@we...> - 2003-06-17 17:51:13
|
Hi Ken,
=20
You can easily replace the default error message by defining a =
MessageSource, i.e. a "messages.properties" bundle or the like. If you =
define a message for the key "typeMismatch" there, you'll see that =
message in your form in case of an error with code "typeMismatch". You =
can even define more specific messages like for "typeMismatch.myField" =
or even "typeMismatch.myObject.myField" that will only get used on the =
specified field resp. object and field. Of course this works with your =
own error codes that your own validator produces too. See the FieldError =
javadoc for details.
=20
So I'd suggest to stick to CustomDateEditor but override the respective =
messages. This will both keep the offending entry and provide a =
localized message. Is that what you meant to achieve? If there's =
anything left, let's try to extend CustomDateEditor accordingly.
=20
Juergen
=20
=20
-----Original Message-----
From: Ken Krebs [mailto:kk...@kk...]
Sent: Tuesday, June 17, 2003 7:32 PM
To: j=FCrgen h=F6ller [werk3AT]
Cc: 'spring-dev-list'
Subject: MVC binding and validation
Juergen,
NOTE: This little problem is not pressing as I have an acceptable =
solution in place for petclinic which will be posted Real Soon Now ;-) =
.
In a couple of my petclinic forms, I use BaseCommandController's =
initBinder method to install a custom Date PropertyEditor to handle =
dates of the form "yyyy-mm-dd". The framework class CustomDateEditor is =
not convenient to subclass so I used it's code as a basis for my own =
SimpleDateEditor (code is shown below). In this class's setAsText =
method, I handle the ParseException to set the object to null because I =
do not like the error message that gets bound in if I let it throw an =
IllegalArgumentException as does CustomDateEditor. The message looks =
like a stack trace and would be incomprehensible to the average end =
user, i.e.: Failed to convert property value of type [java.lang.String] =
to required type [java.util.Date]; nested exception is: =
java.lang.IllegalArgumentException: Could not parse date: Unparseable =
date: "2000-06-". This message comes from BeanWrapperImpl's =
setPropertyValue throwing a TypeMismatchException. Setting the object =
value to null allows the problem to be handled by my validator which =
simply reports the now erased date entry as "invalid". Obviously, this =
is not ideal. It would be better to allow the PropertyEditor to provide =
a user-friendly, localizable exception while preserving the offending =
entry. Unfortunately, setAsText can't throw any checked Exceptions like =
the ErrorCodedPropertyVetoException that DataBinder's bind method uses.
What do you think ?
Ken
/*
* SimpleDateEditor.java
*
*/
package petclinic.validation;
import java.util.Date;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.text.ParseException;
/**
*
* @author Ken Krebs
*/
public class SimpleDateEditor extends PropertyEditorSupport {
=20
private SimpleDateFormat dateFormat;
=20
/** Creates a new instance of SimpleDateEditor */
public SimpleDateEditor() {
dateFormat =3D new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
}
=20
/**
* Parse the Date from the given text, using the specified =
DateFormat.
* Trap a ParseException to allow the Validator to handle it.
*/
public void setAsText(String text) {
try {
setValue(this.dateFormat.parse(text));
} catch (ParseException ex) {
// throw new IllegalArgumentException("Could not parse =
date: " + ex.getMessage());
setValue(null);
}
}
=20
/**
* Format the Date as String, using the specified DateFormat.
*/
public String getAsText() {
return this.dateFormat.format((Date) getValue());
}
=20
}
|