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.

Appending checkbox to beginning of checkbox list

  1. sanz
    Member

    Hey all ... just wondering if I'm encountering a bug or not with gform_pre_render.

    Using the following gform_pre_render filter to populate checkboxes:

    add_filter("gform_pre_render_6", 'selected_location_checkboxes');
    add_filter("gform_admin_pre_render_6", 'selected_location_checkboxes');
    function selected_location_checkboxes($form){
    	global $current_user;
    	get_currentuserinfo();
    
        $choices = array();
        $inputs = array();
    	$args = array( 'hide_empty' => 0, 'taxonomy'=> 'location'); // custom taxonomy
        $categories = get_categories($args);
    
    array_push($choices, array("text" => 'All/None', "value" => '', "isSelected" => false));
    array_push($inputs, array("label" => 'All/None', "id" => 'selectAllLocations'));
    
    	$i = 1;
    	foreach ($categories as $category) {
    		$field_id = "17.".$i;
    		array_push($choices, array("text" => $category->cat_name, "value" => $category->term_id, "isSelected" => (is_object_in_term( $current_user->ID, 'location', $category ) ) ? true : false ));
    		array_push($inputs, array("label" => $category->cat_name, "id" => $field_id));
    		$i++;
    	}
    
        foreach($form["fields"] as &$field){
            if($field["id"] == 17){
                $field["choices"] = $choices;
                $field["inputs"] = $inputs;
            }
        }
    
        return $form;
    }

    As can be seen I'm attempting to add an initial checkbox with All/None as the text and no value and not selected.

    It gets output as a checkbox on the front end however for some reason it is always marked as "checked". It's also automatically given an id of "choice_17_1" and NOT "selectAllLocations" as I need.

    I'm trying to output a checkbox that can be used to select/deselect all other checkboxes on this field and need one checkbox that is not by default selected and also with a id of my choosing.

    Anyone know if this is a bug or am I missing something? Anyone done this before?

    Thanks

    Scott

    Posted 9 years ago on Monday April 9, 2012 | Permalink
  2. Hey Scott, anyway we could see a link to the form too?

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

    Hey Rob ... unfortunately the form is not publicly accessible at the moment.

    I've ended up using jQuery to unselect the single checkbox on display and am not worrying about the id for now.

    I think there may still be an issue there however I'll have to send through the form manually to you after I've finished the project I'm working on.

    Could also just be my crappy code messing up somewhere ;-)

    Thanks anyway.

    Scott

    Posted 9 years ago on Wednesday April 11, 2012 | Permalink