I want to set up a form on the front-end of a site to allow logged in users to choose items from a list which represent entries in a channel using checkboxes, then save those entries as relations to an entry that the user is editing.
Using the docs and various questions here I have got setting a relationship to one entry working. However, I want to allow up to 5 relationships to be set and I can't figure out how to add further relationships.
The entry field containing the related choices is called choices
.
Here's the template code I've got at the moment (simplified):
{# Get current list of choices to pre-set checkboxes #}
{% set choices = entry.choices %}
{% set choiceList = '' %}
{% for choice in choices %}
{% set choiceList = choiceList ~ choice.id ~ "," %}
{% endfor %}
<form id="entry-edit-form" method="post" accept-charset="UTF-8">
{{ getCsrfInput() }}
<input type="hidden" name="action" value="entries/saveEntry">
<input type="hidden" name="redirect" value="choices-saved">
<input type="hidden" name="sectionId" value="8">
<input type="hidden" name="enabled" value="1">
<input type="hidden" name="entryId" value="{{ entry.id }}">
<input type="hidden" id="choicesList" name="fields[choices][]" value="">
{% for choice in choices %}
<div>
<input class="choice-checkbox" type="checkbox" id="choice{{ loop.index }}" data-id="{{ choice.id }}" {% if choice.id in (choiceList|split(',')) %}checked="checked"{% endif %} />
<label for="choice{{ loop.index }}">{{ choice.title }}</label>
</div>
{% endfor %}
<input type="submit" class="button" value="Save choices">
</form>
Then I've got some javascript that runs on form submit to loop through the checkboxes and grab the ids of the checked boxes. What format do these need to be saved in as a value to the hidden choicesList
field so that an array of related ids is saved to the correct field in the edited entry?
Or is there another, simpler, way of doing this?
choices[
so the values are ignored and not saved.