Skip to content

Commit a9f4206

Browse files
committed
Generate bracketless tag id in FreeMarker forms
Before this change if FreeMarker Spring form macro was bound to a path which contains square brackets, those brackets would also appear in id of generated tag, making the id invalid. As of this fix all FreeMarker Spring form macros generate tag with id that does not contain square brackets. Issue: SPR-8732
1 parent 9c8332a commit a9f4206

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
-->
158158
<#macro formInput path attributes="" fieldType="text">
159159
<@bind path/>
160-
<input type="${fieldType}" id="${status.expression}" name="${status.expression}" value="<#if fieldType!="password">${stringStatusValue}</#if>" ${attributes}<@closeTag/>
160+
<input type="${fieldType}" id="${status.expression?replace('[','')?replace(']','')}" name="${status.expression}" value="<#if fieldType!="password">${stringStatusValue}</#if>" ${attributes}<@closeTag/>
161161
</#macro>
162162

163163
<#--
@@ -202,7 +202,7 @@
202202
-->
203203
<#macro formTextarea path attributes="">
204204
<@bind path/>
205-
<textarea id="${status.expression}" name="${status.expression}" ${attributes}>${stringStatusValue}</textarea>
205+
<textarea id="${status.expression?replace('[','')?replace(']','')}" name="${status.expression}" ${attributes}>${stringStatusValue}</textarea>
206206
</#macro>
207207

208208
<#--
@@ -218,7 +218,7 @@
218218
-->
219219
<#macro formSingleSelect path options attributes="">
220220
<@bind path/>
221-
<select id="${status.expression}" name="${status.expression}" ${attributes}>
221+
<select id="${status.expression?replace('[','')?replace(']','')}" name="${status.expression}" ${attributes}>
222222
<#if options?is_hash>
223223
<#list options?keys as value>
224224
<option value="${value?html}"<@checkSelected value/>>${options[value]?html}</option>
@@ -244,7 +244,7 @@
244244
-->
245245
<#macro formMultiSelect path options attributes="">
246246
<@bind path/>
247-
<select multiple="multiple" id="${status.expression}" name="${status.expression}" ${attributes}>
247+
<select multiple="multiple" id="${status.expression?replace('[','')?replace(']','')}" name="${status.expression}" ${attributes}>
248248
<#list options?keys as value>
249249
<#assign isSelected = contains(status.actualValue?default([""]), value)>
250250
<option value="${value?html}"<#if isSelected> selected="selected"</#if>>${options[value]?html}</option>
@@ -267,7 +267,7 @@
267267
<#macro formRadioButtons path options separator attributes="">
268268
<@bind path/>
269269
<#list options?keys as value>
270-
<#assign id="${status.expression}${value_index}">
270+
<#assign id="${status.expression?replace('[','')?replace(']','')}${value_index}">
271271
<input type="radio" id="${id}" name="${status.expression}" value="${value?html}"<#if stringStatusValue == value> checked="checked"</#if> ${attributes}<@closeTag/>
272272
<label for="${id}">${options[value]?html}</label>${separator}
273273
</#list>
@@ -288,7 +288,7 @@
288288
<#macro formCheckboxes path options separator attributes="">
289289
<@bind path/>
290290
<#list options?keys as value>
291-
<#assign id="${status.expression}${value_index}">
291+
<#assign id="${status.expression?replace('[','')?replace(']','')}${value_index}">
292292
<#assign isSelected = contains(status.actualValue?default([""]), value)>
293293
<input type="checkbox" id="${id}" name="${status.expression}" value="${value?html}"<#if isSelected> checked="checked"</#if> ${attributes}<@closeTag/>
294294
<label for="${id}">${options[value]?html}</label>${separator}
@@ -307,7 +307,7 @@
307307
-->
308308
<#macro formCheckbox path attributes="">
309309
<@bind path />
310-
<#assign id="${status.expression}">
310+
<#assign id="${status.expression?replace('[','')?replace(']','')}">
311311
<#assign isSelected = status.value?? && status.value?string=="true">
312312
<input type="hidden" name="_${id}" value="on"/>
313313
<input type="checkbox" id="${id}" name="${id}"<#if isSelected> checked="checked"</#if> ${attributes}/>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ public void testForm16() throws Exception {
260260
assertTrue("Wrong output: " + output, output.contains("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"checked\" />"));
261261
}
262262

263+
@Test
264+
public void testForm17() throws Exception {
265+
assertEquals("<input type=\"text\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", getMacroOutput("FORM17"));
266+
}
267+
263268
private String getMacroOutput(String name) throws Exception {
264269

265270
String macro = fetchMacro(name);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ FORM15
8686

8787
FORM16
8888
<@spring.formCheckbox "command.jedi"/>
89+
90+
FORM17
91+
<@spring.formInput "command.spouses[0].name", ""/>

0 commit comments

Comments
 (0)