-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Hamid Badiozamani opened SPR-10837 and commented
In Spring 3.1.4 (and possibly other releases) the springFormCheckboxes macro iterates through the $status.value variable to determine whether or not an item needs to be checked or not.
We ran into an issue with the checkboxes not properly reflecting the form backing object. Let's say the model attribute variable "command" has a variable "command.userSelections" which is of type Map<Integer, List<Integer>> and $possibleSelectionList which is of type Map<String, Integer> exposed in the model.
Iterating the command.userSelections map and using $i as the key if we use:
#springFormCheckboxes( "command.userSelections[$i]" $possibleSelectionList "" "")
We would not see any of the checkboxes as having been selected regardless of the value in command.userSelections[$i]. After further investigation it appears that the culprit is here:
spring.vm:263
#foreach($item in $status.value)
#if($item == $option) checked="checked" #end
#end
The $status.value is converted to a String representation of the contents of the bound variable which in this case happens to be a List<Integer> and as such VTL is unable to traverse it in the #foreach loop above.
We used the following workaround:
#**
- springFormCheckboxes
- Show checkboxes.
-
@param
path the name of the field to bind to -
@param
options a map (value=label) of all the available options -
@param
separator the html tag or other character list that should be used to - separate each option. Typically ' ' or '<br>'
-
@param
attributes any additional attributes for the element (such as class - or CSS styles or size
*#
#macro( springFormCheckboxes $path $options $separator $attributes )
#springBind($path)
#set( $literal = '#set(' )
#set( $pathValueEval = "$literal$path)" )
#evaluate( $pathValueEval )
#foreach($option in $options.keySet())
<input type="checkbox" name="${status.expression}" value="${option}"
#foreach($item in $pathValue)
#if($item == $option) checked="checked" #end
#end
${attributes} #springCloseTag()
${options.get( {separator}Extra close brace or missing open brace
$option)} $
#end
<input type="hidden" name="_${status.expression}" value="on"/>
#end
By using $pathValueEval to set the variable $pathValue we're able to circumvent String conversion and get a List that can be properly iterated.
Affects: 3.1.4
Referenced from: commits 3af9d1f, 4f60b98
Backported to: 3.2.8
1 votes, 3 watchers
Activity
spring-projects-issues commentedon Jan 31, 2014
Dirk Lachowski commented
What about changing
to
spring-projects-issues commentedon Jan 31, 2014
Juergen Hoeller commented
Good point. I've revised this for consistent iteration over the actualValue in our Velocity and FreeMarker macros now. We did it for FreeMarker before but not for Velocity... and we lacked exposure of an actualValue if no BindingResult is available (e.g. for a first-time rendering of a form, driven by a GET request).
Juergen