|
From: Ken K. <kk...@kk...> - 2003-07-16 04:15:03
|
Thomas, Juergen showed me a way to handle a similar situation for Petclinic's Date entry requirements. <Juergen> 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. </Juergen> The message gets triggered via an IllegalArgumentException thrown by the PropertyEditor. The handler that catches the IllegalArgumentException and triggers the message is doTypeConversionIfNecessary() in BeanWrapperImpl. You may also want to have a look at beans.propertyeditors.CustomNumberEditor. Petclinic's AbstractClinicForm shows how to install a property editor in it's override of BaseCommandController.initBinder(). Regards, Ken tri...@tr... wrote: >Ken and All, > >Are there any examples where the HTML Form has some input fields that are used >to enter numeric values. What is the best way to handle non numeric input for >numeric fields? I'm trying to bind to a commandObject that has a field that is >an integer. > >I have tried using an int and it works if I enter a number but if I leave the >field blank or enter an non nueric value, then I get the following error message >from the bind during validation: > >Failed to convert property value of type [java.lang.String] to required type >[int]; nested exception is: java.lang.NumberFormatException: For input string: "" > >Using an Integer it does not work at all - I get this error message: > >Failed to convert property value of type [java.lang.String] to required type >[java.lang.Integer]; nested exception is: java.lang.IllegalArgumentException: >argument type mismatch > >Thomas > > > > |