Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Aug 14, 2013 · 2 comments

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 14, 2013

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 31, 2014

Dirk Lachowski commented

What about changing

#foreach($item in $status.value)

to

#foreach($item in $status.actualValue)

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants