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.

gform_pre_render and multiselect

  1. I am generating a multiselect field based on subcategories.

    add_filter('gform_pre_render_1', 'amenities');
    function amenities($form){
    
    	$amenities = get_categories(array('child_of' => 15, 'order_by' => 'name', 'hide_empty' => 0));
    	$amenities_choices = array();
    	foreach ($amenities as $amenity) {
    		$amenities_choices[] = array('text' => $amenity->name, 'value' => $amenity->term_id);
    	}
    
    	foreach($form['fields'] as &$field) {
    		if($field['id'] == 12) {
    			$field['choices'] = $amenities_choices;
    		}
    	}
    
    	return $form;
    }

    This works great. The problem is that on the creation of the form, I only have one "item" on the multiselect. So when processing Gforms only checks for one. I am looking for something that can be adjusted via a filter so it will process numbers based on the newly generated multiselect options.

    Posted 12 years ago on Thursday August 23, 2012 | Permalink
  2. Found another filter that works. "gform_pre_submission_filter_1" You also have to modify the "inputs" array for it to work though.

    add_filter("gform_admin_pre_render", "amenities");
    add_filter("gform_pre_submission_filter_1", "amenities");
    add_filter('gform_pre_render_1', 'amenities');
    function amenities($form){
    
    	$amenities = get_categories(array('child_of' => 15, 'order_by' => 'name', 'hide_empty' => 0));
    	$amenities_choices = array();
    	$amenities_inputs = array();
    	$i = 1;
    	foreach ($amenities as $amenity) {
    		$amenities_choices[] = array('text' => $amenity->name, 'value' => $amenity->term_id);
    		$amenities_inputs[] = array('id' => '12.'.$i, 'label' => $amenity->name, 'name' => '');
    		$i++;
    	}
    
    	foreach($form['fields'] as &$field) {
    		if($field['id'] == 12) {
    			$field['choices'] = $amenities_choices;
    			$field['inputs'] = $amenities_inputs;
    		}
    	}
    	//ri_echo($form);
    	return $form;
    }
    Posted 12 years ago on Thursday August 23, 2012 | Permalink