4

How to use select dropdown with entries/saveEntry for an entry field?

I have an entry with a field that is another entry, which I want to update with a form:

<form method="post" accept-charset="UTF-8">
  {{ getCsrfInput() }}
  <input type="hidden" name="action" value="entries/saveEntry">
  <input type="hidden" name="entryId" value="{{ entry.id }}">
  <label>Vendor
    <select name="fields[vendor][]">
      <option>---</option>
      {% for v in craft.entries.section('vendors') %}
        <option value="{{v.id}}">{{v.title}}</option>
      {% endfor %}
    </select>
  </label>
  <input type="submit" value="Submit"/>
</form>

This works fine, except if nothing is selected in the dropdown (<option>---</option>). In that case, the POST value for fields[vendor][] is ---, and I get a SQL foreign key constraint error.

I have also tried <option value="">---</option> and <option value="NULL">---</option> with no better outcome.

Marion Newlevant
  • 12,047
  • 22
  • 55
  • Been a while since I've dealt with this, but I did come up with this same error. Try adding selected="selected" to the option. I think the issue is submitting a field with empty data. The form isn't submitted with that option selected. – Chase Giunta Feb 17 '16 at 19:16
  • @ChaseGiunta - pretty sure the selected=selected only affects which choice is initially selected, and nothing in the post data. In any event, it didn't help (unfortunately) – Marion Newlevant Feb 17 '16 at 19:29

1 Answers1

3

I ran into this a while ago and it drove me nuts. Try adding disabled and selected attributes to your initial option in the select. So in your case <option disabled selected>---</option>.

Chase Giunta
  • 749
  • 5
  • 14
  • This is perfect, except... once you have selected a different option there is no going back to the --- one. Probably need javascript for that, but my problem is solved. – Marion Newlevant Feb 17 '16 at 20:29