Answers.Body: <p>Assuming you are storing these as civi option values, you could do it the way civi does it with the wrench icon to add new options on-the-fly. There's a data-option you add to the wrench tag.</p>
  <p>If you prefer it more like the contact entity refs with the &quot;New&quot; options, you can almost do it with addEntityRef, but it would need to support the &quot;create&quot; option in props, e.g. <code>$form-&gt;addEntityRef('gender_id', 'Gender', ['entity' =&gt; 'OptionValue', 'create' =&gt; TRUE, 'api' =&gt; ['params' =&gt; ['option_group_id' =&gt; 3]]]);</code> (the &quot;create&quot; doesn't work there). But you might be able to civix generate your own entity, and make that work. You want to validate it server-side, so it's not just a matter of setting the select2 &quot;tags&quot; option. (I'm sure you've seen the &quot;an illegal choice has been detected&quot; message in drupal.)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-27T15:19:57.673Z
Answers.Text: I think it has to be an option group. If it was an entity reference, a new entry would spawn a new entity, which seems dangerous and messy.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-27T15:21:49.827Z
Answers.Text: I think the answer I thought I was looking for is in here: https://stackoverflow.com/questions/31199507/tagging-text-in-jquery-select2 i.e. the "tags" option for select2 requires a text input field, so with a little custom js, I can just apply the select2 to my text field and somehow tell it to use my option group as the option list.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-27T15:24:09.360Z
Answers.Text: re: your last sentence - in fact, that's exactly the challenge, I don't want server-side validation, I want the user to be allowed to put anything in the box, I just want to encourage them to re-use an existing value (or rather, one of a curated list of values).
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-27T16:53:49.117Z
Answers.Text: In this case the "entity" is OptionValue. But I see you're saying you don't want visitors to be allowed to add values to the suggestion list.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-26T22:56:56.203Z
Answers.LastActivityDate: 2024-03-26T22:56:56.203Z
Answers.UserDisplayName: Demerit
Body: <p>I have a field which normally expects a value selected from a managed list (e.g. a list of courses, a list of colleges). The client would like the visitor to be prompted with one of the managed list as they type in their entry, to promote consistency in spelling, etc., but not to restrict the value with this UI. In other words, it should allow any text entry.</p>
  <p>So that means, I think I want a plain text field with an implementation of the select2 widget that doesn't require the entry of one of the select options.</p>
  <p>Note that this isn't the same as the classic select-or-other where you have a select field followed by a hidden text field that becomes visible when other is selected, and in some ways seems like it should be easier.</p>
  <p>This kind of UI is <a href="https://select2.org/tagging" rel="nofollow noreferrer">natively supported by the select2 widget</a></p>
  <p>Any clues on implementation?</p>
  
Tags: <custom-fields><select2>
Title: How can I implement a select-or-other widget over a plain text field?
ThreadText: # How can I implement a select-or-other widget over a plain text field?
  
  (Asked by: username_0 on 2024-03-26)
  
  I have a field which normally expects a value selected from a managed list (e.g. a list of courses, a list of colleges). The client would like the visitor to be prompted with one of the managed list as they type in their entry, to promote consistency in spelling, etc., but not to restrict the value with this UI. In other words, it should allow any text entry.
  
  So that means, I think I want a plain text field with an implementation of the select2 widget that doesn't require the entry of one of the select options.
  
  Note that this isn't the same as the classic select-or-other where you have a select field followed by a hidden text field that becomes visible when other is selected, and in some ways seems like it should be easier.
  
  This kind of UI is [natively supported by the select2 widget](https://select2.org/tagging)
  
  Any clues on implementation?
  
  **Tags:** custom-fields, select2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-03-26. Score: 1
  
  Assuming you are storing these as civi option values, you could do it the way civi does it with the wrench icon to add new options on-the-fly. There's a data-option you add to the wrench tag.
  
  If you prefer it more like the contact entity refs with the "New" options, you can almost do it with addEntityRef, but it would need to support the "create" option in props, e.g. `$form->addEntityRef('gender_id', 'Gender', ['entity' => 'OptionValue', 'create' => TRUE, 'api' => ['params' => ['option_group_id' => 3]]]);` (the "create" doesn't work there). But you might be able to civix generate your own entity, and make that work. You want to validate it server-side, so it's not just a matter of setting the select2 "tags" option. (I'm sure you've seen the "an illegal choice has been detected" message in drupal.)
  
  > **Comments:**
  > **username_0**: I think it has to be an option group. If it was an entity reference, a new entry would spawn a new entity, which seems dangerous and messy.  
  > **username_0**: I think the answer I thought I was looking for is in here: https://stackoverflow.com/questions/31199507/tagging-text-in-jquery-select2 i.e. the "tags" option for select2 requires a text input field, so with a little custom js, I can just apply the select2 to my text field and somehow tell it to use my option group as the option list.  
  > **username_0**: re: your last sentence - in fact, that's exactly the challenge, I don't want server-side validation, I want the user to be allowed to put anything in the box, I just want to encourage them to re-use an existing value (or rather, one of a curated list of values).  
  > **username_1**: In this case the "entity" is OptionValue. But I see you're saying you don't want visitors to be allowed to add values to the suggestion list.  
  
  ---
  
  