PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamic Checkboxes

  1. dimensionmedia
    Member

    Very simply, i'm attempting to use filters/hooks to populate the values and names of a set of checkboxes on a form on my site (which is embedded via the php gravity_form() function).

    I've searched through the forum and I've seen examples for dropdowns and text boxes, but nothing on checkboxes. If i've missed something specific, please point me in the first direction.

    This is what I have so far, but of course this is totally wrong. And yes, I did check off the dynamic option for my checkboxes and assigned 'photos' as the param.

    add_filter("gform_field_value_photos", "populate_photos");
    function populate_photos(){
    $items[] = array("value" => 'test', "text" => 'test1');
    return $items;
    }

    Thanks in advance!

    Posted 14 years ago on Sunday September 5, 2010 | Permalink
  2. The checkbox field is surprisingly complex and we unfortunately don't have all the hooks in place to enable it to be populated dynamically without any limitations. The following code snippet is the best I could come up with. It will allow the checkboxes to be added to your form and you will be able to view the entry in the admin detail screen, but you won't be able to change the entry and update the checkbox values. You also won't be able to view the checkbox values in the entry list.
    It is by no means a perfect solution, but like I said, it is the best we can do right now and it might be good enough for you.
    I am going to keep this in mind and try to add the necessary hooks in the next versions so that checkboxes can be populated dynamically.

    //Adds a filter to form id 26. Replace 26 with your actual form id
    add_filter("gform_pre_render_26", populate_checkbox);
    add_filter("gform_admin_pre_render_26", populate_checkbox);
    
    function populate_checkbox($form){
    
        //Creating choices
        $choices = array(
                        array("text" => "Test 1", "value" => "test1"),
                        array("text" => "Test 2", "value" => "test2"),
                        array("text" => "Test 3", "value" => "test3")
                        );
    
        $inputs = array(
                        array("label" => "Test 1", "id" => 2.1), //replace 2 in 2.1 with your field id
                        array("label" => "Test 2", "id" => 2.2), //replace 2 in 2.2 with your field id
                        array("label" => "Test 2", "id" => 2.3), //replace 2 in 2.3 with your field id
                    );
    
        //Adding items to field id 2. Replace 2 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field){
            //replace 2 with your checkbox field id
            if($field["id"] == 2){
                $field["choices"] = $choices;
                $field["inputs"] = $inputs;
            }
        }
    
        return $form;
    }
    Posted 14 years ago on Monday September 6, 2010 | Permalink
  3. dimensionmedia
    Member

    Alex, Thanks. I'll give this a shot. I wanted to create checkboxes on the fly, and really didn't care about viewing or doing else via the admin. It would be great if we could open checkboxes up more in the future though.

    Posted 14 years ago on Sunday September 12, 2010 | Permalink
  4. Is there any more progress on this? I have a form that I'd like to populate radio buttons dynamically based on checkboxes from a previous step. The smartest way I can think of so far is to use the javascript 'next page' hook to hide/show multiple choice options based on selected checkboxes from the previous step. I was hoping for something more elegant.

    Posted 13 years ago on Wednesday January 19, 2011 | Permalink
  5. Hi m4change,

    Just confirmed the lead dev. The gform_pre_render hook is still your best bet for dynamically populating checkboxes. Not sure what is in store for checkbox population, but we will keep you posted via the blog of any upcoming features for this.

    Posted 13 years ago on Thursday January 20, 2011 | Permalink

This topic has been resolved and has been closed to new replies.