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
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

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

  • #10788 Velocity springFormCheckbox results in checkboxes that are always checked

Referenced from: commits 93e9955

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 14, 2008

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

@spring-projects-issues spring-projects-issues commented Sep 20, 2008

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

@spring-projects-issues spring-projects-issues commented Nov 12, 2008

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

@spring-projects-issues spring-projects-issues commented Jan 14, 2009

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

@spring-projects-issues spring-projects-issues commented Aug 4, 2009

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

@spring-projects-issues spring-projects-issues commented Aug 4, 2009

Dave Syer commented

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

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 4, 2009

Arjen Poutsma commented

Patches applied.

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