Answers.Body: <p>I suspect what you're seeing is a permissions problem. You mentioned it doesn't work for Front-End users but it does work on the back-end.</p>
  <p>EntityRef fields work by calling Apiv3 via ajax. For that to work, a user must have either &quot;access CiviCRM&quot; or &quot;access ajax api&quot; permission. Then they must also have permission to see the entities you are referencing. To view contacts, that usually means the &quot;view all contacts&quot; permission.</p>
  <p>You definitely don't want to give all those permissions to anonymous users! Fortunately there is a new solution to this problem: switch from <code>$this-&gt;addEntityRef()</code> to <code>$this-&gt;addAutocomplete()</code>. The new Autocomplete widget uses APIv4 and allows you finer-grained control over permissions. You do not need to grant users any special permission to use it, instead you can give non-permissioned users access to a particular widget via an event listener.</p>
  <p>Here are docs on that: <a href="https://docs.civicrm.org/dev/en/latest/framework/autocomplete/" rel="nofollow noreferrer">https://docs.civicrm.org/dev/en/latest/framework/autocomplete/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-31T13:31:35.960Z
Answers.Text: Thanks for this.  I had to update the permissions since adding the addAutocomplete() was not defined in my version.  I likely need to update my Civi version.  This will be temporary until I convert this form to the new format.
Answers.UserDisplayName: Norm Dressler
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-30T21:34:34.107Z
Answers.LastActivityDate: 2023-07-30T21:34:34.107Z
Answers.UserDisplayName: Coleman
Body: <p>I'm using Civicrm 5.58.1 on Wordpress 6.2.2.  I've had a QuickForm working for quite some time.</p>
  <p>$this-&gt;addEntityRef('Location_province', ts('Select Province'),
  ['entity' =&gt; 'state_province','api' =&gt; ['params' =&gt; ['country_id' =&gt; 1039]]]);</p>
  <p>$this-&gt;addEntityRef('trial_chairperson', ts('Select Trial Chairperson'));</p>
  <p>$this-&gt;addEntityRef('trial_secretary', ts('Select Trial Secretary'));</p>
  <p>All of these now return a &quot;Loading Failed&quot; message to the front end user.  User cannot override and enter any text there and these are required fields so the form is now broken.</p>
  <p>I know I should update this form to Afform and Angular, but Quickform is easy and its been working for years.  Not sure what update would have possibly broken it?  Any ideas what I can do to solve this for now?</p>
  <p>Thanks
  Norm</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-07-30T20:04:43.087Z
Comments.Text: What version did you upgrade from when it stopped working?
Comments.UserDisplayName: Coleman
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-07-30T21:24:27.983Z
Comments.Text: I have not upgraded and n a while.  More info, I just checked the corresponding back end form and it works?  Now I'm confused
Comments.UserDisplayName: Norm Dressler
Tags: <quickform><entityref>
Title: Quickform AddEntityRef Errors 5.58
ThreadText: # Quickform AddEntityRef Errors 5.58
  
  (Asked by: username_0 on 2023-07-30)
  
  I'm using Civicrm 5.58.1 on Wordpress 6.2.2. I've had a QuickForm working for quite some time.
  
  $this->addEntityRef('Location\_province', ts('Select Province'),
  ['entity' => 'state\_province','api' => ['params' => ['country\_id' => 1039]]]);
  
  $this->addEntityRef('trial\_chairperson', ts('Select Trial Chairperson'));
  
  $this->addEntityRef('trial\_secretary', ts('Select Trial Secretary'));
  
  All of these now return a "Loading Failed" message to the front end user. User cannot override and enter any text there and these are required fields so the form is now broken.
  
  I know I should update this form to Afform and Angular, but Quickform is easy and its been working for years. Not sure what update would have possibly broken it? Any ideas what I can do to solve this for now?
  
  Thanks
  Norm
  
  **Tags:** quickform, entityref
  
  > **Comments:**
  > **username_1**: What version did you upgrade from when it stopped working?  
  > **username_0**: I have not upgraded and n a while.  More info, I just checked the corresponding back end form and it works?  Now I'm confused  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-07-30. Score: 2 | Accepted answer
  
  I suspect what you're seeing is a permissions problem. You mentioned it doesn't work for Front-End users but it does work on the back-end.
  
  EntityRef fields work by calling Apiv3 via ajax. For that to work, a user must have either "access CiviCRM" or "access ajax api" permission. Then they must also have permission to see the entities you are referencing. To view contacts, that usually means the "view all contacts" permission.
  
  You definitely don't want to give all those permissions to anonymous users! Fortunately there is a new solution to this problem: switch from `$this->addEntityRef()` to `$this->addAutocomplete()`. The new Autocomplete widget uses APIv4 and allows you finer-grained control over permissions. You do not need to grant users any special permission to use it, instead you can give non-permissioned users access to a particular widget via an event listener.
  
  Here are docs on that: <https://docs.civicrm.org/dev/en/latest/framework/autocomplete/>
  
  > **Comments:**
  > **username_0**: Thanks for this.  I had to update the permissions since adding the addAutocomplete() was not defined in my version.  I likely need to update my Civi version.  This will be temporary until I convert this form to the new format.  
  
  ---
  
  