I have a contact form (public facing only) on my site that has a checkbox group. The user can pick their favorite color(s). Upon successful submission I am capturing that data within the CP via a plugin.
I have a checkbox field within the CP called "colors".
This is how I am outputting the values in my template:
<input type="text" id="name" name="name"class="form-control" value="{% if contact is defined %}{{ contact.name }}{% endif %}" placeholder="Full Name"/> {{ contact is defined and contact ? errorList(contact.getErrors('name')) }} .... {% for option in craft.fields.getFieldByHandle('colors').getFieldType.options %} <div class="checkbox"> <label> <input type="checkbox" name="fields[colors][]" value="{{ option.value }}">{{ option.label }} </label> </div> {% endfor %} .... So far so good. Name is required, so if a user submits the form without entering a name, but has chosen a few colors, I would like the colors to be checked when the page reloads. I am able to do that on the name field easily.
The next part of the question, how do I handle the array of options via my plugin?
Here is what I have in my controller:
ContactFormController
.... $contact->profession = craft()->request->getPost('profession[]'); When I log what the model is getting here is what I am seeing:
[name] => My Name [colors] => .... I assume that I will also need to set up a FK relationship in my record file so once the form is submitted I can have a 1:many relationship. I'll look at the docs on how to set that up - and post what I've done back for future reference.