[2.0.3-java] binding for multiple checkboxes

1,254 views
Skip to first unread message

Shane Witbeck

unread,
Aug 6, 2012, 6:35:40 PM8/6/12
to play-fr...@googlegroups.com
I'm attempting to bind multiple values from a group of checkboxes. I've tried multiple combinations including setting the name attributes to an indexed value (for example: selectedSamples[0], selectedSamples[1], etc.)

In my bean I have:

List<String> selectedSamples = new ArrayList<String>();

When I debug the value coming to the controller from the form, I just get a single value (for example: selectedSamples[2] = "other") even though multiple checkboxes are checked.

Is there a known issue with binding multiple checkbox values or am I missing something?

An example in Java on how to do this would be awesome.

Kevin Bosman

unread,
Aug 6, 2012, 6:50:28 PM8/6/12
to play-fr...@googlegroups.com

Have you tried setting your checkbox name attributes to selectedSamples[], ie. without a numeric index?

Shane Witbeck

unread,
Aug 6, 2012, 7:04:24 PM8/6/12
to play-fr...@googlegroups.com
Yes, with that I get the following exception:

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NumberFormatException: For input string: ""]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.2]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.2]
at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.2]
at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) ~[na:1.6.0_33]
at java.lang.Integer.parseInt(Integer.java:470) ~[na:1.6.0_33]
at java.lang.Integer.parseInt(Integer.java:499) ~[na:1.6.0_33]
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:979) ~[spring-beans.jar:3.0.7.RELEASE]
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:921) ~[spring-beans.jar:3.0.7.RELEASE]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76) ~[spring-beans.jar:3.0.7.RELEASE]

Shane Witbeck

unread,
Aug 7, 2012, 9:21:36 AM8/7/12
to play-fr...@googlegroups.com
After further debugging, I've found what I believe is a bug in the play.data.Form class. Specifically, lines ~111 to 116:

 for(String key: urlFormEncoded.keySet()) {
            String[] value = urlFormEncoded.get(key);
            if(value.length > 0) {
                data.put(key, value[0]);
            }
        }

So in my case the urlFormEncoded map has the correct data structure which is a String[] of the selected values but the above code only ever grabs the first array element.

Shane Witbeck

unread,
Aug 7, 2012, 9:26:04 AM8/7/12
to play-fr...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages