• 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

[Struts 2]s:checkbox fieldValue always true in validation before submitting

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, I use <s:checkbox> to pass boolean value into my Action class in Struts 2. Before submitting data, I need validation of data.

In my action class, I find that in validation method, all boolean value passed by <s:checkbox> are always true. But in submitting, all is correct.

I tried to use jQuery functions to change the fieldValue of all checkboxs according to their boolean value, to no avail. All javascript codes are executed, but fieldValue are not changed, all true.

See more details in my question in Stackoverflow:

Stackoverflow question

But there are no answers to this, so I put it here. Thanks.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for linking the Stack Overflow question. It saves all of us time and effort.
First:

Basically, we are submitting twice the form, firstly for validation, then for submitting.


Why would you do such a thing? Why not just process the form if it passes validation?

As for your issue, it is a quirk of HTML that unchecked checkboxes are not submitted in the form. If the server receives a checkbox, the value of it is assumed to be "true". Look at the HTML source in the browser. You'll have a regular HTML checkbox as well as a hidden field with a similar name. When the form is submitted, the hidden field will be submitted even if false, and Struts uses that to set the "false" values on the server side. I think you need to be setting the hidden field values rather than the checkboxes (though I think it would Just Work if you weren't tinkering around with the double submit).
 
Nope Torres
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Ess wrote:Thanks for linking the Stack Overflow question. It saves all of us time and effort.
First:

Basically, we are submitting twice the form, firstly for validation, then for submitting.


Why would you do such a thing? Why not just process the form if it passes validation?

As for your issue, it is a quirk of HTML that unchecked checkboxes are not submitted in the form. If the server receives a checkbox, the value of it is assumed to be "true". Look at the HTML source in the browser. You'll have a regular HTML checkbox as well as a hidden field with a similar name. When the form is submitted, the hidden field will be submitted even if false, and Struts uses that to set the "false" values on the server side. I think you need to be setting the hidden field values rather than the checkboxes (though I think it would Just Work if you weren't tinkering around with the double submit).



Thanks for the effort. I have found that the problem lies in my javascript function that doesn't process checkboxs. Detail in my OP in Stackoverflow, for someone may experience the same.

http://stackoverflow.com/questions/35062446/scheckbox-fieldvalue-always-true-in-validation-before-submitting#answer-35128388
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic