Skip to content

Velocity springFormCheckboxes macro does not check preselected items correctly [SPR-10837] #15463

@spring-projects-issues

Description

@spring-projects-issues

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( $pathValue = $' )
    #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($option)} ${separator}
    #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

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions