• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DynaValidatorActionForm and checkboxes

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have declared a form using DynaValidatorForm.
One of its properties is bound to a checkbox, the form scope is request and I have provided an initial value of false to this same prop.
The problem is that my checkbox is not holding its unselected stated when I deselect it (originally seleceted) and submit the form.
Please could anyone help me on this issue ?
Thanks a lot !
Juarez Junior
 
Ranch Hand
Posts: 314
2
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
DynaActionForm classes (and their subclasses) invoke the 'reset' method by default, and as a result its fields will always be set to their initial value.
You have a number of options open to you:
1. Make sure the pre-set the values of each field in the DynaActionForm before passing it onto an Action class. They may not be appropriate depending on what you are doing.
2. Subclass the DynaValidatorActionForm and override its 'reset' method so that it behaves as you wish. You can then use it as the base class of your form definitions in the 'struts-config.xml' file.
Cheers,
Darryl
 
Darryl A. J. Staflund
Ranch Hand
Posts: 314
2
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
Reading your message again, I am not sure I addressed your issue in my last posting. Sorry for the red herring :-)
Darryl
 
Juarez Alvares Barbosa Junior
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Darryl,
Thanks for helping !
I'm going to check if I am doing the right thing, that is, if the prop type and its initial value are correct.
Best,
Juarez
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best answer I have seen to this was from Chris Felaco on jguru:

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

I will quote it here just in case:
---
There is a very simple solution to the checkbox problem. The trick is to always add a hidden field AFTER the checkbox with the same parameter name but with the value set to "false":

<html:checkbox property="booleanProperty" value="true" />
<input type="hidden" name="booleanProperty" value="false">

Basically, the hidden parameter ensures that there is some request parameter submitted, which will always set the corresponding ActionForm property. If the checkbox is checked, the 2 parameters will be passed: "booleanProperty=true&booleanProperty=false". But the beanutils code only uses the first parameter to set the value. If the checkbox is left unchecked, then there will be only 1 parameter "booleanProperty=false" which will ensure the property is set.

The HTML spec says that form elements must be submitted in the GET or POST in the order that they are specified in the HTML, and all browsers I have used obey this rule. There is no risk that the hidden parameter will obscure the form field as long as you always put it after the checkbox.

The beauty of this solution is that it places the burden in the hands of the page author where there can be more flexibility. It also allows the page author to invert the meaning of the checkbox without bothering the application developer. All the page author needs to do is reword the caption, and switch the ordering of the values used in the checkbox and hidden fields. Also, it works with string or numeric properties just as easily.
---
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic