Answers.Body: <p>This can be done by adding javascript directly to the TPL file associated with the QuickForm.</p>
  
  <p>In the FORM php file The following are the elements that I add for selecting:</p>
  
  <pre><code>        $this-&gt;addEntityRef('field_dc_group', ts('Select DC for the Leader'), array(
          'entity' =&gt; 'group',
          'api' =&gt; array(
              'params' =&gt; array(
                  //'tag' =&gt; GetCiviSetting('org.namelessnetwork.smallgrouptracking', 'small_group_leader_tag'), // filters results to just those with Small Group Leader Tag
                  'parents' =&gt; array('IN' =&gt; array(34)), // this will get the DC we want to select
              )),
          'select' =&gt; array(minimumInputLength =&gt; 0),// auto populate results),
          'id' =&gt; 'field_dc_group'), 
              true // require 
      );
  
      $this-&gt;addEntityRef('field_sg_group', ts('Select Small Group Leader'),array(
          'entity' =&gt; 'group',
          'id' =&gt; 'field_sg_group', 
          'select' =&gt; array(minimumInputLength =&gt; 0),
          ),
              true
      );`
  </code></pre>
  
  <p>At the bottom of the FORM tpl file I added the following javascript code (inside {literal} tag):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      CRM.$(function ($) {
  
  
        console.log($("#field_dc_group").val);
  
        $("#field_dc_group").change(function() {
          $('#field_sg_leader').select2('data', {id: null, text: null}); // clear currently selected group
            console.log("DC Group ID: " + $("#field_dc_group").val());
          $('[name=field_sg_group]').crmEntityRef({
              entity: 'group',
              api: {params: {parents: $("#field_dc_group").val()}},
              create: true
          });
  
        });
        $( document ).ready(function() {
          $('#field_sg_group').hide();
        });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>Using this setup I am able to update the select2 fields dynamically as well as get the value in the FORM postProcess.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-11T20:32:07.793Z
Answers.LastActivityDate: 2017-04-13T14:51:44.123Z
Answers.LastEditDate: 2017-04-13T14:51:44.123Z
Answers.UserDisplayName: Sleewok
Body: <p>I am building a form that has an entityRef that gets me a list of groups. When a group is selected I want another entityRef to be displayed that uses the value of from the entityRef selection (I'm getting the child groups for the selection). </p>
  
  <p>I'm looking for some direction on the best approach to this. </p>
  
  <p>Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-10T00:31:47.223Z
Comments.Text: Have you seen https://forum.civicrm.org/index.php?topic=34423.0 ?
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-11T12:57:55.793Z
Comments.Text: Demerit, yeah that was actually posted by me :). In that case is was on the JavaScript side. What I'm trying to do here is is on the PHP side using a PHP form. I'm starting to think I just need to rewrite it all to run on the client side.
Comments.UserDisplayName: Sleewok
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-07-11T18:47:00.267Z
Comments.Text: I'd agree about going client-side. Unless you mean something like have someone select from your groups field, then submit the form, then another field/page appears based on the selection. Then you could do it all in php/quickform via controllers like the way the import pages work.
Comments.UserDisplayName: Demerit
Tags: <quickform><entityref>
Title: Dynamically Change QuickForm on EntityRef selection
ThreadText: # Dynamically Change QuickForm on EntityRef selection
  
  (Asked by: username_0 on 2016-07-08)
  
  I am building a form that has an entityRef that gets me a list of groups. When a group is selected I want another entityRef to be displayed that uses the value of from the entityRef selection (I'm getting the child groups for the selection).
  
  I'm looking for some direction on the best approach to this.
  
  Thanks!
  
  **Tags:** quickform, entityref
  
  > **Comments:**
  > **username_1**: Have you seen https://forum.civicrm.org/index.php?topic=34423.0 ?  
  > **username_0**: username_1, yeah that was actually posted by me :). In that case is was on the JavaScript side. What I'm trying to do here is is on the PHP side using a PHP form. I'm starting to think I just need to rewrite it all to run on the client side.  
  > **username_1**: I'd agree about going client-side. Unless you mean something like have someone select from your groups field, then submit the form, then another field/page appears based on the selection. Then you could do it all in php/quickform via controllers like the way the import pages work.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-07-11. Score: 2 | Accepted answer
  
  This can be done by adding javascript directly to the TPL file associated with the QuickForm.
  In the FORM php file The following are the elements that I add for selecting:
          $this->addEntityRef('field_dc_group', ts('Select DC for the Leader'), array(
          'entity' => 'group',
          'api' => array(
              'params' => array(
                  //'tag' => GetCiviSetting('org.namelessnetwork.smallgrouptracking', 'small_group_leader_tag'), // filters results to just those with Small Group Leader Tag
                  'parents' => array('IN' => array(34)), // this will get the DC we want to select
              )),
          'select' => array(minimumInputLength => 0),// auto populate results),
          'id' => 'field_dc_group'), 
              true // require 
      );
  
      $this->addEntityRef('field_sg_group', ts('Select Small Group Leader'),array(
          'entity' => 'group',
          'id' => 'field_sg_group', 
          'select' => array(minimumInputLength => 0),
          ),
              true
      );`
  
  At the bottom of the FORM tpl file I added the following javascript code (inside {literal} tag):
  
  
  Using this setup I am able to update the select2 fields dynamically as well as get the value in the FORM postProcess.
  
  
  ---
  
  