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.

Checkboxes pre_render and populated dynamic

  1. Hi,

    I have not problem creating a dynamic checkboxes with this code.

    add_filter("gform_pre_render", "populate_dropdown_users");
    
    function populate_dropdown_users($form)
    {
    	// recollim usuaris registrats
    	global $wpdb;
    	$allAuthorIds = $wpdb->get_col( "SELECT ID FROM {$wpdb->users}" );
    	$authorsInfo = array();
    	$items = array();
    	foreach( $allAuthorIds as $authorId ) {
      		$authorsInfo[$authorId] = get_userdata($authorId);
      		if($authorsInfo[$authorId]->user_level==0)
      		{
      			$items[] = array("value" => $authorId, "text" => "<img src=\"http://www.gravatar.com/avatar/".md5($authorsInfo[$authorId]->user_email)."?s=30\" class=\"img_avatar\">".$authorsInfo[$authorId]->display_name);
      		}
    	}
    
    	foreach($form['fields'] as &$field)
    	{
    
    		// buscarem els que tenen cssClass == custom_field_usuaris
    	 	 if(strpos($field['cssClass'],'custom_field_usuaris')===false)
                continue;
    
              $field["choices"] = $items;
    
    	}
    
    	 return $form; // retornem el formulari
    
    }

    But when I tried to create a form with this checkboxes and populated dynamically a content, only first element is selected.

    Example.

    $field_values = array();
    $field_values['projProfe'] = "2,3";
    gravity_form(2, false, false, $display_inactive=false, $field_values, false);

    Only checkboxes with 2 value is checked not, checbox with value 3 is checked. If I do this with a checkbox with no pre_render content work correctly.

    Any idea?

    thanks.

    Posted 12 years ago on Tuesday November 15, 2011 | Permalink
  2. Dynamically creating a checkbox list is pretty complex. Being able to display it on the form is not a problem, but the admin side of things (email notification, entry list, entry detail, etc...) will not work correctly unless you write a lot of code to handle the data display in all different areas.
    If you really want to tackle this, I can point you in the right direction, but it will take considerable amount of PHP code to get it done.

    Posted 12 years ago on Tuesday November 15, 2011 | Permalink
  3. My problem not is in email notification, entry list, entry detail, because in this case not use.

    I think that the problem is that an incompatible problem with populated dynamically and a checkbox that is filled dynamically.

    David.

    Posted 12 years ago on Wednesday November 16, 2011 | Permalink
  4. I understand.
    The easiest way to select them is to add the "is_selected" => true property when creating the inputs.

    See below:

    $items[] = array("value" => $authorId, "isSelected"=>true, "text" => "<img src=\"http://www.gravatar.com/avatar/".md5($authorsInfo[$authorId]->user_email)."?s=30\" class=\"img_avatar\">".$authorsInfo[$authorId]->display_name);
    Posted 12 years ago on Wednesday November 16, 2011 | Permalink
  5. Alex, this work, but is not a solutio for me. I need to select checkbox from a dynamic value. I can know $field_values inside gform_pre_render hook?

    $field_values = array();
    $field_values['projProfe'] = "2,3";
    gravity_form(2, false, false, $display_inactive=false, $field_values, false);
    Posted 12 years ago on Friday December 2, 2011 | Permalink