PDA

View Full Version : Checkbox Shortcoming



nhusher
Sep 7th, 2007, 12:05 PM
Hello all,

I'm going to preface this by saying that I am not well-versed in Spring. I understand a bunch of the concepts, I understand Java, I just haven't had time to dive into Spring. Instead, my specialization is in html/css/javascript as a frontend person.

My question--and perhaps complaint--is in regards to the way Spring handles checkboxes. I am writing the HTML frontend for a web application, and it appears that the only way to have a working checkbox in Spring is to have a checkbox tailed with a hidden field to compensate for Spring's insistence that bound inputs actually return data. As a designer who strives for semantic, simple, and maintainable markup, this irks me.

Is there any way around this? If not, why isn't Spring capable of absorbing incomplete inputs from checkboxes?


Links:
http://timchalk.wordpress.com/2007/07/18/getting-checkboxes-to-work-correctly-with-spring-form-validation/
http://www.springframework.org/docs/reference/mvc.html
etc...

nllarson
Sep 7th, 2007, 01:19 PM
The two links you give use two different tags.

The first link uses the old <spring:bind> tag wrapped around the checkbox tag. This one did require the hidden text box after it to work properly. (I am assuming you are using these tags, since you have to specify the hidden text box on your pages)

The second link, the <form:checkbox> tag is used. This tag should generate the hidden box for you. You should try using this set of tags (<form:*).

Nick

nhusher
Sep 7th, 2007, 01:22 PM
Both examples still produce the hidden tag, that's what I was trying to get at. Surely there's some way of elminating the need for the hidden tag altogether, right?

nllarson
Sep 7th, 2007, 02:22 PM
Both examples still produce the hidden tag, that's what I was trying to get at. Surely there's some way of eliminating the need for the hidden tag altogether, right?

Sorry...I misunderstood your question. I thought you didn't want to have to write out the hidden box afterward. And the form: tags allow you to do that. As far as removing them completely and still have them work correctly....I would not know....sorry...

Nick

Jörg Heinicke
Sep 7th, 2007, 02:47 PM
The reason for the hidden input is that unchecked checkboxes don't end as parameter in the request. Spring's data binding works on the request parameters. Without one for the checkbox value there is no chance to get the value unset on the bean.

Joerg

jglynn
Sep 7th, 2007, 04:46 PM
Correct.

In order for Spring to assume that the absence of a parameter for a given checkbox field should indicate a status of "unchecked" you would need to explicitly inform the binder of every field it would need deal with in this manner.

IMO, this pains me more than the slight markup bastardization.

Good first post, welcome to the forum.