Please Note: We are no longer using forums to facilitate standard support. 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 Checkbox from Custom Taxonomy

  1. * Purely an informative post for the community, no need for an Admin response *

    I just spent a little while editing the code provided by the admin's on how to dynamically populate checkboxes so I could create a list of services from a custom taxonomy. It was pretty straight-forward but I just thought I'd post it incase it saves someone 20 minutes in the future.

    All the best,
    Andy

    //Adds a filter to form id 3, change number 3 to your form ID.
    add_filter("gform_pre_render_3", populate_checkbox);
    add_filter("gform_admin_pre_render_3", populate_checkbox);
    
    function populate_checkbox($form){
    
    	//Creating choices and inputs as empty arrays
        $choices = array();
        $inputs = array();
    
    	$categories=  get_categories('taxonomy=services'); // change services to the name of your taxonomy
    
    	$i = 1; // we need a counter to create the field id
    
            // loop through the array and push the results to the $choices and $inputs arrays
    	foreach ($categories as $category) {
                    // field_id identify's which element this is going to replace. Change 2 to whatever your field id should be
    		$field_id = "2.".$i;
    
    		array_push($choices, array("text" => $category->cat_name, "value" => $category->cat_name));
    		array_push($inputs, array("label" => $category->cat_name, "id" => $field_id));
    
    		$i++; // add 1 to the counter for the next loop
    	}
    
        //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 3 years ago on Monday October 31, 2011 | Permalink
  2. Thank you for posting your code.

    Posted 3 years ago on Tuesday November 1, 2011 | Permalink
  3. Thanks man, Helps a lot!

    Posted 3 years ago on Monday November 7, 2011 | Permalink
  4. brian.s
    Member

    This code populates the field dynamically, but entirely overwrites the Gravity Field definition in the Form Editor. Furthermore, the code fails the validation for "required" even though one or more boxes are checked.

    It would help if more thorough documentation was available for dynamically populating form fields using Gravity Forms intended methods, especially fields with multiple choices.

    Posted 3 years ago on Wednesday November 9, 2011 | Permalink
  5. ptroxler
    Member

    Excellent instruction, solved my problem.

    NOTE when using 1.6.2
    $field_id = "2.".$i;
    should read
    $field_id = "2_".$i;

    Posted 3 years ago on Monday April 9, 2012 | Permalink
  6. raffav
    Member

    how i can get checked [some, all] checkbox?

    Posted 2 years ago on Thursday October 25, 2012 | Permalink
  7. @raffav, please being a new topic and ask your complete question there. If it's related to the code posted by @squidgemann, you can find their contact details on their profile page:

    http://www.gravityhelp.com/forums/profile/squidgemann

    Posted 2 years ago on Thursday October 25, 2012 | Permalink

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

Support Tickets

Open A New Support Ticket
 Open A New Priority Support Ticket *

* Developer license holders only