|
From: =?iso-8859-1?Q?<jp....@ti...> - 2003-08-20 16:43:51
|
Ken,=0D=0A=0D=0AYou highlight a second issue (remainings bindings). The f=
irst was due in my case as you could see by the use of nested properties.=
=0D=0AThe best way for this second issue would be, I think, to rewrite th=
e BeanWrapperImpl in order to catch and store exceptions during the bindi=
ng and then rethrow the first one when all bindings are done.=0D=0A=0D=0A=
Jean-Pierre=0D=0A=0D=0A---------- Initial Header -----------=0D=0A=0D=0AF=
rom : Ken Krebs <kk...@kk...>=0D=0ATo : "jp.pawlak@tiscali=
.fr" <jp....@ti...>=0D=0ACc : "j=FCrgen h=F6ller [werk3A=
T]"<jue...@we...>, 'spring-dev-list' <springframework-deve=
lo...@li...>=0D=0ADate : Wed, 20 Aug 2003 10:43:29 -=
0500=0D=0ASubject : Re: [Springframework-developer] Tags=0D=0A=0D=0AJP,=0D=
=0A=0D=0APetclinic shows an example of this on the add/edit Pet forms and=
the =0D=0AaddVisit form. Look at the messages.properties for the typeMis=
match =0D=0Amessages.=0D=0A=0D=0AI got a reply from Juergen when I querie=
d about this situation (sent on =0D=0A17-JUN). See my original message an=
d the reply below.=0D=0A=0D=0AAs to keeping the data intact, your FormCon=
troller should set the =0D=0AsessionForm property to true. In petclinic, =
this is done in the =0D=0Aconstructor. The date editor is setup in Abstra=
ctClinicForm.initBinder().=0D=0A=0D=0AThere is a problem with this techni=
que in that this process will retain =0D=0Athe bad date data that was ent=
ered but not other subsequent data on the =0D=0Aform as it seems to short=
circuit the rest of the binding process. Try =0D=0Aentering something in=
the Description field on the addVisit form. Then =0D=0Aenter a bad date,=
submit and see what happens. Perhaps I'm not doing it =0D=0Aquite right =
??=0D=0A=0D=0AInitially, I tried to work around the ugly stacktrace messa=
ge I was =0D=0Agetting by creating my own date editor and trapping the Pa=
rseException =0D=0Aand setting the field to null, thereby handing it off =
to the validator. =0D=0AIdeally, it would be nice if the custom editor wo=
uld participate in the =0D=0Asame way as validators do, IMO.=0D=0A=0D=0AR=
egards,=0D=...@ti... wrote:=0D=0A=0D=0A=
>Hi,=0D=0A>I have a basic problem with the form validation. Ok for reject=
Value from a validator, but how to handle Mismatch errors from the binder=
itself? =0D=0A>Assume using a date field that is malformed by the user. =
The editor will not be able to parse the date. Currently I come back to t=
he form page as expected. But:=0D=0A>1) The malformed data is lost. The f=
ield is empty.=0D=0A>2) The status.errorMessage is void also.=0D=0A>=0D=0A=
>How not loose the initial input and how be able to write an error messag=
e to the user?=0D=0A>=0D=0A>Sorry for a so basic question, but if I canno=
t find the solution, it will be worse for new users.=0D=0A>=0D=0A>Jean-Pi=
erre=0D=0A>=0D=0A>=0D=0A> =0D=0A>=0D=0A=0D=0A=0D=0A=0D=0AJuergen's reply=
:=0D=0A=0D=0A> =0D=0A>=0D=0AHi Ken,=0D=0A =0D=0AYou can easily replace t=
he default error message by defining a =0D=0AMessageSource, i.e. a "messa=
ges.properties" bundle or the like. If you =0D=0Adefine a message for the=
key "typeMismatch" there, you'll see that =0D=0Amessage in your form in =
case of an error with code "typeMismatch". You =0D=0Acan even define more=
specific messages like for "typeMismatch.myField" =0D=0Aor even "typeMis=
match.myObject.myField" that will only get used on the =0D=0Aspecified fi=
eld resp. object and field. Of course this works with your =0D=0Aown erro=
r codes that your own validator produces too. See the FieldError =0D=0Aja=
vadoc for details.=0D=0A =0D=0ASo I'd suggest to stick to CustomDateEdito=
r but override the respective =0D=0Amessages. This will both keep the off=
ending entry and provide a =0D=0Alocalized message. Is that what you mean=
t to achieve? If there's =0D=0Aanything left, let's try to extend CustomD=
ateEditor accordingly.=0D=0A =0D=0AJuergen=0D=0A =0D=0A =0D=0A-----Origin=
al Message-----=0D=0A*From:* Ken Krebs [mailto:kk...@kk...]=0D=0A*Sent:*=
Tuesday, June 17, 2003 7:32 PM=0D=0A*To:* j=FCrgen h=F6ller [werk3AT]=0D=
=0A*Cc:* 'spring-dev-list'=0D=0A*Subject:* MVC binding and validation=0D=0A=
=0D=0AJuergen,=0D=0A=0D=0ANOTE: This little problem is not pressing as I =
have an acceptable =0D=0Asolution in place for petclinic which will be po=
sted Real Soon Now ;-) .=0D=0A=0D=0AIn a couple of my petclinic forms, I=
use BaseCommandController's =0D=0AinitBinder method to install a custom =
Date PropertyEditor to handle =0D=0Adates of the form "yyyy-mm-dd". The f=
ramework class CustomDateEditor is =0D=0Anot convenient to subclass so I =
used it's code as a basis for my own =0D=0ASimpleDateEditor (code is show=
n below). In this class's setAsText =0D=0Amethod, I handle the ParseExcep=
tion to set the object to null because I =0D=0Ado not like the error mes=
sage that gets bound in if I let it throw an =0D=0AIllegalArgumentExcepti=
on as does CustomDateEditor. The message looks =0D=0Alike a stack trace a=
nd would be incomprehensible to the average end =0D=0Auser, i.e.: *Failed=
to convert property value of type [java.lang.String] =0D=0Ato required t=
ype [java.util.Date]; nested exception is: =0D=0Ajava.lang.IllegalArgumen=
tException: Could not parse date: Unparseable =0D=0Adate: "2000-06-"*. Th=
is message comes from BeanWrapperImpl's =0D=0AsetPropertyValue throwing a=
TypeMismatchException. Setting the object =0D=0Avalue to null allows the=
problem to be handled by my validator which =0D=0Asimply reports the now=
erased date entry as "invalid". Obviously, this =0D=0Ais not ideal. It w=
ould be better to allow the PropertyEditor to provide =0D=0Aa user-friend=
ly, localizable exception while preserving the offending =0D=0Aentry. Unf=
ortunately, setAsText can't throw any checked Exceptions like =0D=0Athe =
ErrorCodedPropertyVetoException that DataBinder's bind method uses.=0D=0A=
=0D=0AWhat do you think ?=0D=0A=0D=0A=0D=0A=0D=0AKen=0D=0A=0D=0A=0D=0A/*=0D=
=0A * SimpleDateEditor.java=0D=0A *=0D=0A */=0D=0A=0D=0Apackage petclinic=
.validation;=0D=0A=0D=0Aimport java.util.Date;=0D=0Aimport java.beans.Pro=
pertyEditorSupport;=0D=0Aimport java.text.SimpleDateFormat;=0D=0Aimport j=
ava.text.ParseException;=0D=0A=0D=0A/**=0D=0A *=0D=0A * @author Ken Kre=
bs=0D=0A */=0D=0Apublic class SimpleDateEditor extends PropertyEditorSupp=
ort {=0D=0A =0D=0A private SimpleDateFormat dateFormat;=0D=0A =0D=0A=
/** Creates a new instance of SimpleDateEditor */=0D=0A public Sim=
pleDateEditor() {=0D=0A dateFormat =3D new SimpleDateFormat("yyyy-=
MM-dd");=0D=0A dateFormat.setLenient(false);=0D=0A }=0D=0A =0D=
=0A /**=0D=0A * Parse the Date from the given text, using the spe=
cified DateFormat.=0D=0A * Trap a ParseException to allow the Valida=
tor to handle it.=0D=0A */=0D=0A public void setAsText(String text=
) {=0D=0A try {=0D=0A setValue(this.dateFormat.parse(te=
xt));=0D=0A } catch (ParseException ex) {=0D=0A // t=
hrow new IllegalArgumentException("Could not parse =0D=0Adate: " + ex.get=
Message());=0D=0A setValue(null);=0D=0A }=0D=0A }=0D=
=0A =0D=0A /**=0D=0A * Format the Date as String, using the spec=
ified DateFormat.=0D=0A */=0D=0A public String getAsText() {=0D=0A=
return this.dateFormat.format((Date) getValue());=0D=0A }=0D=0A=
=0D=0A}=0D=0A=0A=0A********** L'ADSL A 20 EUR/MOIS**********=0ATiscali=
propose l'ADSL le moins cher du march=E9 : 20 EUR/mois et le modem ADSL =
offert ! =0APour profiter de cette offre exceptionnelle, cliquez ici : ht=
tp://register.tiscali.fr/adsl/=0AOffre soumise =E0 conditions.=0A
|