<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>

CC BY-SA 3.0
2016-10-27T11:30:58.480Z
2016-10-27T11:30:58.480Z
Clive Portman
<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>

CC BY-SA 3.0
2016-10-27T11:31:37.953Z
Thanks Matt. Have figured this out - the name attribute is different when the field is empty.
Clive Portman
CC BY-SA 3.0
2016-10-27T10:44:00.983Z
2016-10-27T10:44:00.983Z
Matt Wilcox
<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>

<users><form><frontend>
Frontend form database error with empty Users field
# 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') %}

n/a
{% for teacher in teachers %}
  {{ teacher.name }}
{% endfor %}


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:


But if you want to return a non-empty Users field, the name attribute is different:


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') %}

n/a
{% for teacher in teachers %}
  {{ teacher.name }}
{% endfor %}


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 Matt. Have figured this out - the name attribute is different when the field is empty.  

---

