spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.39k stars 38.06k forks source link

form:option value="" for Integer property causes bind error [SPR-2391] #7080

Closed spring-projects-issues closed 12 years ago

spring-projects-issues commented 18 years ago

Douglas Surber opened SPR-2391 and commented

I am using a form:select tag to select an Integer value. One of the possible options is label="--" value="". This means none selected. In the domain model and the database this is represented as a null value. The ServletRequestDataBinder.bind is barfing resulting in a validation error. It is failing when the CustomNumberEditor tries to convert the empty string to an Integer, throwing NumberFormatException. CustomNumberEditor is able to convert "" to null, but it seems the one registered as the default is configured not to.

I can solve this by adding a CustomNumberEditor configured to support empty strings, but I'll have to register it for all the integer properties that I use. It seems like a bad thing to have to register a custom editor for the common case of coverting "" to (Integer) null in an options list.

I'm not sure what the correct fix would be. Having the default CustomNumberEditor handle empty strings. Having form:select register a CustomNumberEditor that handles empty strings for Integer properties (if it knows enough to do so), or something else. This is going to be a common case and having to register a property editor to make it work is a pain.

A little more detail to help you track down what is happening. PropertyTypeConverter.convertIfNecessary(String,Object, ...) is calling this.propertyEditor.Registry.getDefaultEditor which is returning a CustomNumberEditor. The CustomNumberEditor has been configured with empty = false. When it is asked to convert "" it calls Integer.decode which throws NumberFormatException. If the default CustomNumberEditor had been configured with empty = true, no problem. I have no idea whether that simple change would be a valid fix. It could cause other problems.


Affects: 2.0 RC3

spring-projects-issues commented 18 years ago

Andreas Schildbach commented

However this issue is resolved, it should be handled uniformly for all pre-registered PropertyEditors. Is there currently any pre-registered editor that allows null / empty text?

spring-projects-issues commented 18 years ago

Rick Evans commented

Juergen is changing the default behaviour of the CustomNumberEditor such that the empty string is accepted (c.f. the allowEmpty ctor arg).

spring-projects-issues commented 18 years ago

Juergen Hoeller commented

Spring's default property editors for primitive wrapper types (Boolean, Integer, etc) turn empty Strings into null values now. This has been a repeated requested and is a sensible default behavior.

In terms of backwards compatibility, this means that empty Strings will now get turned into null values where they previously led to an exception. However, since I doubt that anyone relied on the exception thrown there before, this shouldn't matter too much. Custom property editors still override those default editors.

Juergen