Skip to main content
February 3, 2011
Question

cfinput checkboxes with same name in form with preservedata

  • February 3, 2011
  • 4 replies
  • 3619 views

I have a form with preservedata="yes" and a bunch of checkboxes with the same name.  When you submit the form and it needs to reload because of a validation error or something, all of the checkboxes with the same names are checked, if just one was checked.  Not just the ones that were checked.

Seems like a bug but im not sure.  Right now the only workaround I can see is to change to a standard input, add a cfif and cfparam

<cfparam name="FORM.test" type="string" default="" />

<input type="checkbox" name="test" id="test1" value="Test1"<cfif ListFindNoCase(FORM.test, "Test1")> checked</cfif>> Testing Checkbox

<input type="checkbox" name="test" id="test2" value="Test2"<cfif ListFindNoCase(FORM.test, "Test2")> checked</cfif>> Testing Checkbox

<input type="checkbox" name="test" id="test3" value="Test3"<cfif ListFindNoCase(FORM.test, "Test3")> checked</cfif>> Testing Checkbox

Any ideas how to fix this so i can use cfinput.  Having to change the names to unique names will not work for me. I need in the list format like it give being the same name.

Thanks

    This topic has been closed for replies.

    4 replies

    BKBK
    Community Expert
    Community Expert
    February 4, 2011

    I don't think it is a bug. ColdFusion is simply making the best of the situation.

    Form elements are posted by name, not by id. Yet you distinguish the checkbox IDs and not the names.

    By design, you should give distinct checkbox elements distinct names. Especially when the action page needs to distinguish between the submitted values, as in your case. It is then a simple matter to modify the field names in the action page.

    Inspiring
    February 4, 2011

    I do not think we have enough information to make a determination on naming convention. However, I do not think it matters. It is still a bug IMO.

    It is valid for multiple checkbox controls to have the same name. Therefore this should be handled properly. Checking all boxes, simply because one of them was checked is wrong and goes against expectation. If I saw that behavior on another website, I would think the form was broken.

    BKBK
    Community Expert
    Community Expert
    February 4, 2011

    -==cfSearching==- wrote:

    I do not think we have enough information to make a determination on naming convention.

    i don't understand. What more information could one need?

    February 4, 2011

    Bug # 86178

    February 4, 2011

    Okay, I submitted a bug and I will use that for now.  Thanks for the help guys.

    Brandon

    Inspiring
    February 4, 2011

    Can you post the bug number here? Just for anyone else who happens upon this thread in the future.

    Participant
    February 4, 2011

    Are you confusing <cfinput type="radio"> and <cfinput type="checkbox">?  Groups of radiobuttons usually share the same value for their name attribute.  Is there a reason you want to have multiple checkboxes that post their data under the same name?

    February 4, 2011

    Nope I want checkboxes not radio buttons.  Its easier to manage when you have like 20 boxes of options and say check all that apply.  Then when you post it gives in a comma separated list.

    So from my previous example if I checked all three it would be FORM.test = "test1,test2,test3"

    Brandon

    Participant
    February 4, 2011

    Brand0nM wrote:

    Nope I want checkboxes not radio buttons.  Its easier to manage when you have like 20 boxes of options and say check all that apply.  Then when you post it gives in a comma separated list.

    So from my previous example if I checked all three it would be FORM.test = "test1,test2,test3"

    Brandon

    Makes sense.  I tried it out myself and I'm definitely seeing the behavior you describe.  I don't know if CF8 or CF7 exhibit this behavior.  If not, then it's definitely a bug; if they do then I'd say it really depends on the intentions of the CF team when they implemented the cfform tag.  Who knows?  Sounds like it wouldn't hurt to submit a bug report.

    Here's a demo for a work around that avoids having to use lots of cfif tags:

    <cfform preservedata="true">

         <cfparam name="form.test" default="" />

         <cfoutput>

              <input type="checkbox" name="test"  #(find("1", form.test) ? "checked" : "")# value="1" />

              <input type="checkbox" name="test"  #(find("2", form.test) ? "checked" : "")# value="2" />

              <input type="checkbox" name="test"  #(find("3", form.test) ? "checked" : "")# value="3" />

              <input type="checkbox" name="test"  #(find("4", form.test) ? "checked" : "")# value="4" />

              <p>Options selected: #form.test#</p>

         </cfoutput>

         <cfinput type="submit" name="submit" value="submit" />

    </cfform>

    It's not much better than your original work around, but thanks to the ternary operator, it's a little more concise.

    Edit: switched from using listContains to find, saving 8 characters of space per checkbox input.