Skip to content

Commit 93e9955

Browse files
committed
SPR-4700 - Add single checkbox input macro for Velocity and Freemarker
1 parent 7043dff commit 93e9955

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl

+17
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@
296296
<input type="hidden" name="_${status.expression}" value="on"/>
297297
</#macro>
298298

299+
<#--
300+
* formCheckbox
301+
*
302+
* Show a single checkbox.
303+
*
304+
* @param path the name of the field to bind to
305+
* @param attributes any additional attributes for the element (such as class
306+
* or CSS styles or size
307+
-->
308+
<#macro formCheckbox path attributes="">
309+
<@bind path />
310+
<#local checked><#if status.value?? && status.value?string=="true">true<#else>false</#if></#local>
311+
<#local id><#if status.expression??>${status.expression}<#else>${path}</#if></#local>
312+
<input type="hidden" name="_${id}" value="on"/>
313+
<input type="checkbox" id="${id}" name="${id}" checked="${checked}" ${attributes}/>
314+
</#macro>
315+
299316
<#--
300317
* showErrors
301318
*

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/spring.vm

+15
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,21 @@
269269
<input type="hidden" name="_${status.expression}" value="on"/>
270270
#end
271271

272+
#**
273+
* springFormCheckbox
274+
*
275+
* Show a single checkbox.
276+
*
277+
* @param path the name of the field to bind to
278+
* @param attributes any additional attributes for the element (such as class
279+
* or CSS styles or size
280+
*#
281+
#macro( springFormCheckbox $path $attributes )
282+
#springBind($path)
283+
<input type="hidden" name="_${status.expression}" value="on"/>
284+
<input type="checkbox" id="${status.expression}" name="${status.expression}" checked="#if("$!{status.value}"=="true")true#{else}false#end" ${attributes}/>
285+
#end
286+
272287
#**
273288
* springShowErrors
274289
*

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public void testAllMacros() throws Exception {
140140

141141
TestBean tb = new TestBean("Darren", 99);
142142
tb.setSpouse(new TestBean("Fred"));
143+
tb.setJedi(true);
143144
request.setAttribute("command", tb);
144145

145146
HashMap names = new HashMap();
@@ -192,6 +193,10 @@ public void testAllMacros() throws Exception {
192193
if (tokens[i].equals("FORM11")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
193194
if (tokens[i].equals("FORM12")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
194195
if (tokens[i].equals("FORM13")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
196+
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
197+
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" checked=\"false\" />", tokens[i + 2]);
198+
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
199+
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"true\" />", tokens[i + 2]);
195200
}
196201
}
197202

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ FORM13
7777

7878
FORM14
7979
<@spring.formSingleSelect "command.name", options, ""/>
80+
81+
FORM15
82+
<@spring.formCheckbox "command.name"/>
83+
84+
FORM16
85+
<@spring.formCheckbox "command.jedi"/>

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityMacroTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void testAllMacros() throws Exception {
133133
rc.setContextPath("/springtest");
134134

135135
TestBean tb = new TestBean("Darren", 99);
136+
tb.setJedi(true);
136137
request.setAttribute("command", tb);
137138

138139
HashMap names = new HashMap();
@@ -178,6 +179,10 @@ public void testAllMacros() throws Exception {
178179
//TODO verify remaining output (fix whitespace)
179180
if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
180181
if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
182+
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
183+
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" checked=\"false\" />", tokens[i + 2]);
184+
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
185+
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"true\" />", tokens[i + 2]);
181186
}
182187
}
183188

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/test.vm

+7
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,12 @@ FORM9
5353
FORM10
5454
#springFormHiddenInput("command.name" "")
5555

56+
FORM15
57+
#springFormCheckbox("command.name" "")
58+
59+
FORM16
60+
#springFormCheckbox("command.jedi" "")
61+
62+
5663
ERRORS
5764
#springShowErrors(" " "")

0 commit comments

Comments
 (0)