View Full Version : form:checkbox --> default checked
TerpInMD
Sep 11th, 2007, 01:10 PM
Hello,
This should be easy but has taken longer than I wanted it to. All I want to do is have the checkbox default to checked when the form is rendered no matter what values are in the backing list. Suggestions?
I am using <form:checkbox path="theList" value="someValue" />
Thanks,
blueskies
Sep 11th, 2007, 01:22 PM
I think the only way to get it checked is based on the property of that field in the object. Why don't you want to set the value in the object?
TerpInMD
Sep 11th, 2007, 01:34 PM
Thanks for the quick reply. I am open to anything. Basically I am using that list to get the ID's (string) of the objects I am going to save...the value is the actual object ID. As I type this I am realizing I will likely need an editor to set and get the values. Seems like a lot to go through to just have the default set to checked instead of not checked.
blueskies
Sep 11th, 2007, 02:24 PM
I'm also trying to fully understand the Spring tags. It does seem like something simple you want to do ends up difficult to figure out.
infinity2heaven
Sep 11th, 2007, 04:19 PM
<form:checkbox path="theList"/> will persist the checked value only if theList is of type Boolean/boolean. If your UI has a checkbox, you would most probably have a boolean to represent that in your domain/command objects. If you don't, then revisit your domain model!
If you still want to bind a non-boolean value, then it get's completed. You need to write a custom taglib which generates the value field, something like this
<form:checkbox path="isReadOnly" value="<tags:processReadOnly value="${command.isReadOnly}"/>"/>
processReadOnly is your tag library, where you can write the logic of returning true or false
TerpInMD
Sep 11th, 2007, 04:34 PM
What I have is a list and I am using a checkbox to select and item from a list:
Object1 PropertyA PropertyB <checkbox here value="Object1Id">
Object2 PropertyA PropertyB <checkbox here value="Object2Id">
Object3 PropertyA PropertyB <checkbox here value="Object3Id">
Object4 PropertyA PropertyB <checkbox here value="Object4Id">
where PropertyA PropertyB are properties of the Objects. The checkbox's are bound to an Array list of Strings. So when the form is submited I only get the values that are selected.
All works well except that I want the checkbox's to default to being selected instead of being deselected.
Jörg Heinicke
Sep 11th, 2007, 04:38 PM
If your UI has a checkbox, you would most probably have a boolean to represent that in your domain/command objects. If you don't, then revisit your domain model!
I don't agree with that. A bunch of checkboxes is a valid use case for multi-selection. That seems to be what he wants. PropertyEditor is the correct way to go then.
Joerg
infinity2heaven
Sep 12th, 2007, 02:01 PM
A bunch of checkboxes for multi select is indeed a valid Use Case, I agree Jörg.
It would have been much simpler if the checbox is backed by a boolean value to transfer state (like object1.isSelected) But if it's used for other semantics like being bound to an Id, it gets complicated. You would have to write a Property Editor for the outer Object class type (Cant write for the checkbox itself I think, as it's a string type).
That's just my thought.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.