0


Bit of a newb here. Teaching myself PHP/java/etc. as a go. I've been using THIS using GF's Document page to create a Chained Select that will pull a Company Name(from a previous GF entry) as Parent, but I am having trouble with the Child's (which is the company's Features).

I was pretty proud of myself when I made a got the Parent of my Chained Select to fire using the following code:

add_filter("gform_pre_render_14", "populate_companies");
add_filter("gform_admin_pre_render_14", "populate_companies");
add_filter( "gform_chained_selects_input_choices_14_5_1", "populate_companies");
 function populate_companies( $form, $input_items, $form_id, $field, $input_id, $chain_value ){
  if( $form["id"] != 14 )
  
         return $form;
   
  $items = array();
   
$form_id = '7';
$entries = GFAPI::get_entries( $form_id );

   if (is_array($entries))
{
 foreach($entries as $fcompany) $items[] = array( 'value' => rgar( $fcompany, '4' ), 'text' => rgar( $fcompany, '4' ), 'isSelected' => false );
}
     foreach($form["fields"] as &$field)
        if($field["id"] == 5){
            $field["choices"] = $items;
        }
    return $form;
}

Now, that code works. Maybe it's not pretty, but it works. So the next step would be to get it to populate the Child field. I've been able to get this far, but am stumped...

add_filter("gform_pre_render", "populate_features");
add_filter("gform_admin_pre_render", "populate_features");
add_filter( "gform_chained_selects_input_choices_14_5_2", "populate_features");
 function populate_features( $form, $input_items, $form_id, $field, $input_id, $chain_value ){
  if( $form["id"] != 14 )
  
         return $form;
   
  $items = array();
  $selected_fcompany = $chain_value[ "{$field->id}.1" ];
   if( ! $selected_fcompany ) {
        return $input_choices;
    }
   
$form_id = '7';
$entries = GFAPI::get_entries( $form_id );

   if (is_array($entries))
{
 foreach($entries as $cfeature) $items[] = array( 'value' => rgar( $cfeature, '10' ), 'text' => rgar( $cfeature, '10' ), 'isSelected' => false );
}
     foreach($form["fields"] as &$field)
        if($field["id"] == 5){
            $field["choices"] = $items;
        }
    return $items;
}

I feel the problem is with the chained_value. Heck if I can get it to work through the first foreach in the first snippet that I'll do the dance of joy. Any ideas out there?

Parkbum
  • 26
  • 4

1 Answers1

0

Ended up going with the Gravity Plus plugin. It's been really handy, but can't have more than one on a form, which is kinda lame. It take some work to get it to work, but let me know of you need help!

https://gravityplus.pro/gravity-forms-dynamic-population/

Parkbum
  • 26
  • 4
  • Populate Anything isn't limited to one on a form and can populate pretty much anything. https://gravitywiz.com/documentation/gravity-forms-populate-anything/ – Dave from Gravity Wiz Apr 24 '19 at 15:35
  • Thanks David. I've progressed past this, and these days are actually more likely to implement my own JavaScript to get complicated population done. – Parkbum Jun 05 '19 at 21:22
  • If you haven't used Populate Anything, I'd highly recommend checking it out. It's quite a bit more robust than anything else on the market and I've had many very experienced developers find Populate Anything is much quicker/easier than most custom solutions they've implemented. – Dave from Gravity Wiz Jun 06 '19 at 11:23
  • Thanks David. My end result for this was me learning JavaScript. It was a fairly complicated AJAX. – Parkbum Jun 23 '19 at 19:19