arrayscoldfusioncoldfusion-2018

Coldfusion array from form checkbox


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?


Solution

  • 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
        }