Checkbox problem - checkboxes submitting that are unchecked

Discussion in 'Java' started by Claire, Feb 28, 2005.

  1. Claire

    Claire Guest

    Hello,

    I am having a problem in my struts application with the checkboxes in
    my form. I have an array of checkboxes, some of which may be already
    selected when the form loads. My problem is when the user then
    deselects one or more of these checkboxes and submits the form -
    they're being submitted as if they were still checked, when the user
    has infact deselected them.

    I've heard that this is a common problem, but I don't seem to be able
    to find the problem / resolution with any search criteria. If anybody
    knows the fix for this it would be greatly appreciated (preferably
    without having to use Javascript)

    Many Thanks, Claire

    Here's the HTML for the checkbox part of the form when it's initially
    loaded.

    <table style="width:50%;" cellpadding="0" cellspacing="0">
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Red</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[0].add" value="on"
    checked="checked">
    </td>
    </tr>
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Orange</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[1].add" value="on"
    checked="checked">
    </td>
    </tr>
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Yellow</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[2].add" value="on"
    checked="checked">
    </td>
    </tr>
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Green</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[3].add" value="on">
    </td>
    </tr>
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Blue</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[4].add" value="on">
    </td>
    </tr>
    <tr class="spreadsheetvalue">
    <td align="left" width="85%">Purpler</td>
    <td align="left" width="15%">
    <input type="checkbox" name="cust[5].add" value="on">
    </td>
    </tr>
    </table>
    Claire, Feb 28, 2005
    #1
    1. Advertising

  2. Claire

    Wendy Smoak Guest

    "Claire" <> wrote:

    > I am having a problem in my struts application with the checkboxes in
    > my form.


    http://struts.apache.org/userGuide/struts-html.html#checkbox

    "WARNING: In order to correctly recognize unchecked checkboxes, the
    ActionForm bean associated with this form must include a statement setting
    the corresponding boolean property to false in the reset() method."

    --
    Wendy
    Wendy Smoak, Feb 28, 2005
    #2
    1. Advertising

  3. Claire

    Guest

    Hello,

    Thanks for pointing me in the right direction. My problem was that I
    had not implemented a

    reset (ActionMapping mapping, HttpServletRequest request)

    method. So for those who know Struts, I had to change my DynaActionForm
    and create a normal Action Form which implemented a reset() method. The
    reset method looped through the array, setting the boolean
    (representing the checkbox) to false.

    The reset() method is automatically called before my preload action to
    prepolulate values (so only reset if the array isn't null, otherwise I
    got a NullPointerException) and then again once the form is submitted
    (the crucial bit, initialising checkboxes back to false and then
    populating with the selected form values).

    Many Thanks, Claire
    , Mar 2, 2005
    #3
  4. Claire

    Wendy Smoak Guest

    <> wrote

    > So for those who know Struts, I had to change my DynaActionForm
    > and create a normal Action Form which implemented a reset() method.


    DynaActionForms can have reset methods, too. :)

    (All you needed to do was write a class that extends DynaActionForm,
    implement reset in it, then change struts-config.xml to use your class
    instead of using DAF directly.)

    --
    Wendy
    Wendy Smoak, Mar 2, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. runescience
    Replies:
    0
    Views:
    4,231
    runescience
    Feb 23, 2006
  2. Dan
    Replies:
    5
    Views:
    3,201
    shawpnendu
    May 26, 2009
  3. RVic
    Replies:
    19
    Views:
    1,363
  4. Glenn
    Replies:
    2
    Views:
    113
    Glenn
    Feb 10, 2005
  5. Claire
    Replies:
    4
    Views:
    207
    Claire
    Mar 2, 2005
Loading...

Share This Page