Link to home
Start Free TrialLog in
Avatar of ssherlock
ssherlock

asked on

Getting 'unchecked' value from Struts checkbox

I know that checkbox is only supposed to return a value IF checked but I'm getting a checked value returned all the time.  I'm using a DynaActionForm on a form that does a search i.e. you fill in 2 boxes at the top, click Search and the results are dsplayed below i.e. I can't just re-display the form nicely reset to default values.

When I uncheck the checkbox I expect there to be no reference to it in the dynaactionfom, yet there it is with a value of On!  Which means I cannot check for it correctly in the Action class.

How can I stop this please?

Thanks, Simon
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

If you use a multibox like suggested here:

http://www.jguru.com/faq/view.jsp?EID=925277

then it returns an array of string that DOES match up to what was clicked...ie:  if it isn't selected, it isn't in the array...

Hope this helps...  checkboxes are a pain...  If this doesn't work, I should be able to find another way of doing it in my code somewhere ;-)

Tim
Avatar of ssherlock
ssherlock

ASKER

It seems overkill to try and use multibox for one field (and the article still mentions calling the reset method).  Do I need to look at somehow over riding the form (bearing in mind I'm using a DynaActionForm) that has a reset() method?
As you say, try resetting the checkbox value to false in the reset method of the form...

Hmmm..  I can't for the life of me remember how I fixed this...sorry...I'll have to dig back through my code...
ASKER CERTIFIED SOLUTION
Avatar of ssherlock
ssherlock

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course it helps if you copy and paste the hidden field so the typos don't creep in :)
<html:hidden property="currentLocation" value="false"/>
;-)  Cool :-)

Glad you got it sorted :-)

(I must remeber to bookmark this page for next time I come up against that problem) ;-)

Hehehe

Tim
Although it works, be careful with that solution, it sounds fragile to me (two inputs with same name, only the first get processed, who knows if it's always that way, and if it's going to change in the future).
I suggest you check the code and identify how is it only the first found field with a name is being used.
Just an opinion to add, since you already figured it out.

Another solution (a little more verbose, but safer), is to create your dynaform class. Is very easy, you create a class and extend from DynaActionForm. I've done it to add lots of functionallity to the dynaform and works great (in that custom class you can then reset before submit or whatever you want to do).

Good luck.