Working with Required Checkboxes in CFFORM

I just ran across this today. There is a bug with required checkboxes in CFMX7. Basically they don't work. However, you can get around this by simply writing your own validation using onSubmit. This is a known issue so no need to bug MACR about it. This code block demonstrates the bug:

<cfform format="flash">

   <cfinput type="checkbox" name="agree" label="I Agree" required="true" value="1">
   <cfinput type="submit" name="submit" value="Push the button">

</cfform>

And here is a sample of how to fix it:

<cfform format="flash" onSubmit="if(!agree.selected ){ alert('You must agree!'); return false; }">

   <cfinput type="checkbox" name="agree" label="I Agree" required="true" value="1">
   <cfinput type="submit" name="submit" value="Push the button">

</cfform>

Archived Comments

Comment 1 by David posted on 3/8/2005 at 4:44 AM

Very odd that this didn't get caught in the beta...In fact, very odd that most of the CFFORM bugs you found weren't fixed pre-release...

Comment 2 by Raymond Camden posted on 3/8/2005 at 5:00 AM

Well, from personal experience I can attest to the fact that MACR's QA for CF is very extensive. Obviously they can't catch everything though.

Comment 3 by Andy Allan posted on 3/8/2005 at 3:41 PM

Excellent stuff once again Ray.

Comment 4 by Andrew posted on 4/5/2005 at 1:31 PM

With checkboxes in CFFORM, the FORM fields contain true/false, not the 'value' if checked as with HTML forms.

Is there a way to get the value returned to the server?

Comment 5 by John Farrar posted on 10/2/2005 at 11:35 PM

OK... how about putting checkboxes in CFFormGroup sections? It seems they don't run for me there. I have changed the type to "radio" and the form loads, but if it is changed back to "checkbox" then it doesn't work. ARGHH... you would think I would have found opportunity to find that before they beta ended. Perhaps the guys on the beta could have verified it and then the update would have had a fix for that.

Comment 6 by John Farrar posted on 10/2/2005 at 11:44 PM

To be more specific... it's nested... inside a
CFForm "flash"...
CFFormGroup "Tab..." ...
CFFromGroup "page"
CFLoop query=""

And for some reason the flash doesn't come up... never loads but if the type is changed to radio then it works.

Comment 7 by Raymond Camden posted on 10/3/2005 at 5:22 PM

When you say they don't show up, you mean they just don't render? Can you post your full code, or a simpler version of it?

Comment 8 by John Farrar posted on 10/4/2005 at 12:48 AM

Got the issue figured out... WHEW!!! You cannot have two fields with the same name if they are checkbox type. In HTML you do this and it gives you back a list of all the checked items. Heh... should be labeled "DON'T TRY THIS IN FLASH FORMS". That took care of the issue just fine.

Comment 9 by Andy Carter posted on 7/28/2007 at 7:48 AM

Thanks Ray- worked perfectly!

Comment 10 by Monique Boea posted on 10/26/2010 at 8:27 PM

What if I have multiple checkboxes. Can this work?

Comment 11 by Monique Boea posted on 10/26/2010 at 8:34 PM

I figured it out :

&& !hips.selected && !rcr.selected && !lab.selected && !bio.select