Answers.Body: <p>Figured it out. If you want to return an empty Users field, the field name attribute has to look like this:</p>
  
  <pre><code>&lt;select name="fields[teacher]" value=""&gt;
  </code></pre>
  
  <p>But if you want to return a non-empty Users field, the name attribute is different:</p>
  
  <pre><code>&lt;select name="fields[teacher][]" value="{user.id}"&gt;
  </code></pre>
  
  <p>So, I'm starting with the first example, and using an onChange event to switch the name attribute depending on whether the value is a valid user ID or not. Here's my final template code:</p>
  
  <pre><code>{% set teachers = craft.users.group('teachers') %}
  &lt;select class="ui search dropdown" name="fields[teacher]" id="teacherselect"&gt;
  &lt;option value="nA"&gt;n/a&lt;/option&gt;
  {% for teacher in teachers %}
    &lt;option value="{{ teacher.id }}"&gt;{{ teacher.name }}&lt;/option&gt;
  {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>And the jquery if anyone wants it:</p>
  
  <pre><code>($(this).val() == "nA") ? $(this).attr('name', 'fields[teacher]') : $(this).attr('name', 'fields[teacher][]')
  </code></pre>
  
  <p>This works.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-27T11:30:58.480Z
Answers.LastActivityDate: 2016-10-27T11:30:58.480Z
Answers.UserDisplayName: Clive Portman
Answers.Body: <p>Yep, seen this before. You can't just have an empty value like that because the validation stuff just sees it as a missing field that's required to not be missing. We got around it by adding the n/a value into the field itself in the back end of Craft.</p>
  
  <p>Which doesn't help you for User Groups unless you want to create an 'n/a' group.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-27T11:31:37.953Z
Answers.Text: Thanks Matt. Have figured this out - the name attribute is different when the field is empty.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-27T10:44:00.983Z
Answers.LastActivityDate: 2016-10-27T10:44:00.983Z
Answers.UserDisplayName: Matt Wilcox
Body: <p>Within a frontend Add Product form (using Craft Commerce) I'm having problems with a database error whenever a Users field is left empty.</p>
  
  <p>Here's the field:</p>
  
  <pre><code>{% set teachers = craft.users.group('teachers') %}
  &lt;select name="fields[teacher][]"&gt;
  &lt;option value=""&gt;n/a&lt;/option&gt;
  {% for teacher in teachers %}
    &lt;option value="{{ teacher.id }}"&gt;{{ teacher.name }}&lt;/option&gt;
  {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>I've tried setting the empty value to zero, but no luck. The forms works fine apart from this, so I don't think it's anything specific to Craft Commerce.</p>
  
Tags: <users><form><frontend>
Title: Frontend form database error with empty Users field
ThreadText: # Frontend form database error with empty Users field
  
  (Asked by: username_0 on 2016-10-27)
  
  Within a frontend Add Product form (using Craft Commerce) I'm having problems with a database error whenever a Users field is left empty.
  
  Here's the field:
  
  ```
  {% set teachers = craft.users.group('teachers') %}
  <select name="fields[teacher][]">
  <option value="">n/a</option>
  {% for teacher in teachers %}
    <option value="{{ teacher.id }}">{{ teacher.name }}</option>
  {% endfor %}
  </select>
  
  ```
  
  I've tried setting the empty value to zero, but no luck. The forms works fine apart from this, so I don't think it's anything specific to Craft Commerce.
  
  **Tags:** users, form, frontend
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2016-10-27. Score: 2
  
  Figured it out. If you want to return an empty Users field, the field name attribute has to look like this:
  
  ```
  <select name="fields[teacher]" value="">
  
  ```
  
  But if you want to return a non-empty Users field, the name attribute is different:
  
  ```
  <select name="fields[teacher][]" value="{user.id}">
  
  ```
  
  So, I'm starting with the first example, and using an onChange event to switch the name attribute depending on whether the value is a valid user ID or not. Here's my final template code:
  
  ```
  {% set teachers = craft.users.group('teachers') %}
  <select class="ui search dropdown" name="fields[teacher]" id="teacherselect">
  <option value="nA">n/a</option>
  {% for teacher in teachers %}
    <option value="{{ teacher.id }}">{{ teacher.name }}</option>
  {% endfor %}
  </select>
  
  ```
  
  And the jquery if anyone wants it:
  
  ```
  ($(this).val() == "nA") ? $(this).attr('name', 'fields[teacher]') : $(this).attr('name', 'fields[teacher][]')
  
  ```
  
  This works.
  
  
  ---
  
  ### Answer by username_1 on 2016-10-27. Score: 1
  
  Yep, seen this before. You can't just have an empty value like that because the validation stuff just sees it as a missing field that's required to not be missing. We got around it by adding the n/a value into the field itself in the back end of Craft.
  
  Which doesn't help you for User Groups unless you want to create an 'n/a' group.
  
  > **Comments:**
  > **username_0**: Thanks username_1. Have figured this out - the name attribute is different when the field is empty.  
  
  ---
  
  