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

Add single checkbox input macro for Velocity and Freemarker [SPR-4700] #9377

Closed
spring-projects-issues opened this issue Apr 14, 2008 · 7 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 14, 2008

Sébastien Launay opened SPR-4700 and commented

Currently for creating a single checkbox input we have 3 solutions:

  • use formCheckboxes macro but with a hard coded single element options parameter.
  • bind to the right field and then add two inputs (one for the real input and optionally one for multi page submission).
  • and the dirty one where we manually create the input and fill its contents.

It would be nice to has a formCheckbox macro for a single checkbox.

Indeed, it would be more flexible for:

  • choosing where we want to put the label
  • linking the input to a label element (not possible with the second solution because there is no id attribute).

Affects: 2.5.3

Attachments:

Issue Links:

Referenced from: commits 93e9955

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Sébastien Launay commented

A patch for adding this feature with documentation update.
The binding works for boolean fields (the main case with false/true) and String fields (null/"true").

@spring-projects-issues
Copy link
Collaborator Author

Craig commented

The addition of single checkbox boolean binding would be really great... Just pinging this item because it's been dormant for a while.

@spring-projects-issues
Copy link
Collaborator Author

mariano goyochea commented

I have this solution

<#macro formCheckbox path label attributes="">
<@spring.bind path/>
<#assign id="${spring.status.expression}">
<#assign status="${spring.stringStatusValue}">
<input type="checkbox" id="${id}" name="${id}"
<#if status="true">checked="checked"</#if> ${attributes}
<@spring.closeTag/>
<label for="${id}">${label?html}</label>
</#macro>

@spring-projects-issues
Copy link
Collaborator Author

Vladimir Kozlov commented

mariano,

your solution works only for rendering (if checkbox was initially checked and after them unchecked on the webpage, new value is not posted back to controller). So, handling of FALSE is needed.

I modified your code according to http://opensource.atlassian.com/confluence/spring/display/COOK/Checkboxes+-+Dealing+with+single+checkboxes (i.e. added one line with hidden input: )

<#macro formCheckbox path attributes="">
<@spring.bind path />
<#assign id="${spring.status.expression}">
<#assign status="${spring.stringStatusValue}">
<input type="hidden" name="_${id}" value="false" />
<input type="checkbox" id="${id}" name="${id}"
<#if spring.status.value>checked="checked"</#if>
${attributes}
<@spring.closeTag/>
</#macro>

Now it works in both cases.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Attached patch for Freemarker only (SPR-4700-ftl.patch) with slightly different implementation and a test case. Patch is against spring-webmvc project 3.0.0 (r 1674). I'll try to do the Velocity one as well the same way.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

Attached velocity patch (with test cases) SPR-4700-vm.patch

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Patches applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants