Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues
Collaborator

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( p a t h V a l u e = ' )
    #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(
    Extra close brace or missing open brace

    $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

Activity

spring-projects-issues

spring-projects-issues commented on Jan 31, 2014

@spring-projects-issues
CollaboratorAuthor

Dirk Lachowski commented

What about changing

#foreach($item in $status.value)

to

#foreach($item in $status.actualValue)
spring-projects-issues

spring-projects-issues commented on Jan 31, 2014

@spring-projects-issues
CollaboratorAuthor

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

added
status: backportedAn issue that has been backported to maintenance branches
in: webIssues in web modules (web, webmvc, webflux, websocket)
on Jan 11, 2019
added this to the 4.0.2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @jhoeller@spring-projects-issues

      Issue actions

        Velocity springFormCheckboxes macro does not check preselected items correctly [SPR-10837] · Issue #15463 · spring-projects/spring-framework