|
From: Ken K. <kk...@kk...> - 2003-07-16 20:59:47
|
Thomas, <Thomas> Also, is there a way to distinguish betweeen a blank and an invalid number - I get typeMismatch for both, but sometimes a blank could be OK. </Thomas> If you override initBinder() in your Form to install a CustomNumberEditor, you can specify allowEmpty=true in the constructor to allow an empty String to pass through. You can then handle the empty String in your Validator if you wish. It seems to me that PropertyEditors could be fertile ground for useful extension. I do think we need something more flexible in the way BeanWrapperImpl, PropertyEditors, and Validators coordinate error handling. The typeMismatch error strings are tied to a particular fieldName but the same fieldName could possibly be used on different Forms to fulfill a somewhat different function. It's not clear in my mind yet what the right mechanism for doing this is. Regards, Ken tri...@tr... wrote: >Ken, > >Thanks, that makes it clearer. > >It seems to me though that the bean factories support different default mappings >compared to what is provided in the binding of form fields. I can map to an >Integer in a bean, but not in a form without getting into providing a custom >PropertyEditor. Also, is there a way to distinguish betweeen a blank and an >invalid number - I get typeMismatch for both, but sometimes a blank could be OK. > If we supported Integer, then that should map to a null, and I could do my on >validation in a Validator class. Am I missing something here? > >I have not spent a lot of time looking through the source code, but there seems >to be some additional mapping that we should provide in the default setup. We >don't seem to support mapping a numeric string to a java.math.BigDecimal, but >that is something I think would be handy for entering monetary amounts etc. If >anybody is already working on any of this, let me know - if not I'll spend some >more time adding some functionality. > >Thomas > > > >>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 >>> >>> >>> >>> >>> >>> >> >> > > > > > > |