1

I am trying to figure out the best way to handle this. I have a series of form fields with checkboxes for people to select options. When that gets submitted it turns form.optiongroups into an array. I then check to see if the id of the optiongroup is in the array and set the checked value to true in case there were form errors I want them to retain their checked value. This all works fine.

If I only select one option though it doesn't come through as an array, but just a regular form field. Is there a way I can handle this to make sure it is always an array?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
spacerobot
  • 265
  • 1
  • 5
  • 23
  • See `sameformfieldsasarray` https://helpx.adobe.com/coldfusion/cfml-reference/application-cfc-reference/application-variables.html – SOS Nov 29 '20 at 02:28

2 Answers2

3

Actually, checkboxes get submitted as a list. You must have something else going on that creates the array.

However, to answer your question as asked, you can use ListToArray(). It would be something like this:

if (structkeyexists(form, 'optiongroups') { // if no boxes are checked the variable will be undefined.
    
    if (isArray(form.optiongroups) == false )
        form.optiongroups = ListToArray(form.optiongroups) 
    } else {  
        code for no boxes checked
    }
rrk
  • 15,677
  • 4
  • 29
  • 45
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
0

It could also be done via...

param form.optiongroups = "";

form.optiongroups = ListToArray(form.optiongroups);
James A Mohler
  • 11,060
  • 15
  • 46
  • 72