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.

2
  • 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. 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) Feb 17 '16 at 19:29
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>.

1
  • 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. Feb 17 '16 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.