Skip to content

form:error tag generates HTML id which contains [ and ], when bound to indexed property [SPR-6862] #11528

@spring-projects-issues

Description

@spring-projects-issues

Hans Desmet opened SPR-6862 and commented

When the tag form:errors is bound to an indexed property, it creates an HTML id which contains the characters [ and ]
These characters are not allowed in an id.

A span generated by form:errors in the example, when you type blabla in the textbox:
<span id="countries[Belgium].errors">Failed to convert property ...</span>

This is a similar problem as the one in #11506

The class that acts as command object:


package org.example.entities;

import java.util.LinkedHashMap;
import java.util.Map;

public class Continent {
// key = name of country
// Integer= number of inhabitants
private Map<String, Integer> countries = new LinkedHashMap<String, Integer>();
public Continent() {
countries.put("Belgium", null);
countries.put("Switzerland", null);
}
public void setCountries(Map<String, Integer> countries) {
this.countries = countries;
}
public Map<String, Integer> getCountries() {
return countries;
}
}

The Controller class:


package org.example.web;

import org.example.entities.Continent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ContinentController {
@RequestMapping(value = "/continent.htm", method = RequestMethod.GET)
public String continentForm(Model model) {
Continent continent=new Continent();
model.addAttribute(continent);
return "continent.jsp";
}
@RequestMapping(value = "/continent.htm", method = RequestMethod.POST)
public String personForm(@ModelAttribute Continent continent, BindingResult bindingResult) {
return "continent.jsp";
}
}

continent.jsp


<<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<title>Continent example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form:form commandName="continent">
<c:forEach items="${continent.countries}" var="entry">
<div>
<form:label path="countries[${entry.key}]">${entry.key}</form:label>
<form:input path="countries[${entry.key}]" />
<form:errors path="countries[${entry.key}]" />
</div>
</c:forEach>
<div><input type="submit" /></div>
</form:form>
</body>
</html>


Affects: 3.0 GA

Referenced from: commits 60ff93e

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions