PDA

View Full Version : Radio Button [u]group[/u] on dynamically generated web form


dmosses
Oct 11th, 2005, 06:24 AM
Ok,
I have a simple table, generated dynamically from a collection of user details. Each row corresponds to a user, with a tick box to allow user to be selected. This works fine if I use check boxes, and binds correctly etc.

I'd like to change the implementation slightly to allow only a single user to be selected using radio buttons in a button group. I use...


<c:choose>
<c:when test="${fn:length(users.userList) != '0'}">
<form method="post">
<table>
<tr>
<th><fmt:message key="form.select" /></th>
<th><fmt:message key="user.username" /></th>
<th><fmt:message key="user.firstname" /></th>
<th><fmt:message key="user.surname" /></th>
</tr>
<c:forEach items="${users.userList}" var="user" varStatus="counter">
<tr>
<spring:bind path="users.userList[${counter.index}].selected" >
<td>
<input type="radio"
<c:if test="${status.value}">
checked="checked"
</c:if>
name="${status.expression}" />
</td>
</spring:bind>
<td><c:out value="${user.username}" /></td>
<td><c:out value="${user.firstName}" /></td>
<td><c:out value="${user.surname}" /></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="<fmt:message key="modifyuser.submit" />" align="center" />
<input type="reset" value="<fmt:message key="modifyuser.reset" />" align="center" />
</form>
</c:when>
<c:otherwise>
<fmt:message key="selectuser.nousers" />
</c:otherwise>
</c:choose>



My problem is that this requires the 'name' attribute of each button in the group to be the same. But name="${status.expression}" generates

userList[0].selected
userList[1].selected
etc.
breaking the selection group.


Any ideas of how to get around this?

Thanks in advance.

David.

Colin Yates
Oct 11th, 2005, 07:05 AM
yeah, you don't want to use a Collection :) If you only want a single user selected then your command should have a setUser(User) method. You could use formBackingObject to populate the model with all users.

HTH.

dmosses
Oct 13th, 2005, 08:19 AM
Thanks, that's useful...but I'm quite new to all this.

You could use formBackingObject to populate the model with all users

Could you shed a little light on how that would work? I thought that formBackingObject added a single object to the model called <commandName value>. So how do I add 'all users' to the model using this?

Did you mean referenceData() ? And then I have a command object that binds to the selected user? How would the jsp side of that work?

Many thanks,

David

dlevine
Oct 19th, 2005, 04:32 PM
i assume he does mean "formBackingObject". then use spring bind tags to bind the command.selectUser.id for the path, of the select object, and have the select options generated from the formBacking collection with the ID as the option value.

does that help?