<p>I wanted to follow up with this thread in case someone else came across this. If there is a better solution, let me know and I will update the answer accordingly.</p>

<p>Using the example above, the user filling out the form can choose their favorite color(s).</p>

<p>I should mention that the <code>colors</code> checkbox field was created in the CP, and all values for the checkbox are managed there. That way I can easily loop through the values within my template.</p>

<p><strong>twig/html</strong></p>

<pre><code>&lt;div class="form-group has-feedback"&gt;
    {% for option in craft.fields.getFieldByHandle('colors').getFieldType.options %}
        &lt;div class="checkbox"&gt;
            &lt;label&gt;
                &lt;input type="checkbox" value="{{ option.value }}" name="fields[colors][]" {% if account is defined and option.value in account.colors %}checked{% endif %}&gt;{{ option.label }}
           &lt;/label&gt;
        &lt;/div&gt;
   {% endfor %}
   {% if account is defined %}
       {{ errorList(account.getErrors('colors')) }}
   {% endif %}
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2015-11-25T18:37:11.183Z
2015-11-26T00:04:00.787Z
2015-11-26T00:04:00.787Z
Damon
<p>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.</p>

<p>I have a checkbox field within the CP called "colors".</p>

<p>This is how I am outputting the values in my template:</p>

<pre><code>&lt;input type="text" id="name" name="name"class="form-control" value="{% if contact is defined %}{{ contact.name }}{% endif %}" placeholder="Full Name"/&gt;
{{ contact is defined and contact ? errorList(contact.getErrors('name')) }}

....

{% for option in craft.fields.getFieldByHandle('colors').getFieldType.options %}
&lt;div class="checkbox"&gt;
    &lt;label&gt;
        &lt;input type="checkbox" name="fields[colors][]" value="{{ option.value }}"&gt;{{ option.label }}
    &lt;/label&gt;
&lt;/div&gt;
{% endfor %}
....
</code></pre>

<p>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 <code>name</code> field easily.</p>

<p>The next part of the question, how do I handle the array of options via my plugin?</p>

<p>Here is what I have in my controller:</p>

<p><strong>ContactFormController</strong></p>

<pre><code>....
$contact-&gt;profession = craft()-&gt;request-&gt;getPost('profession[]');
</code></pre>

<p>When I log what the model is getting here is what I am seeing:</p>

<pre><code>[name] =&gt; My Name
[colors] =&gt;
....
</code></pre>

<p>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.</p>

<plugin-development><checkboxes>
How do I handle and/or repopulate checkbox group(s)?
# How do I handle and/or repopulate checkbox group(s)?

(Asked by: username_0 on 2015-09-23)

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:

{{ contact is defined and contact ? errorList(contact.getErrors('name')) }}

....

{% for option in craft.fields.getFieldByHandle('colors').getFieldType.options %}

    
        {{ option.label }}
    

{% 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.

**Tags:** plugin-development, checkboxes

---

## 1 answers

### Answer by username_0 on 2015-11-25. Score: 0 | Accepted answer

I wanted to follow up with this thread in case someone else came across this. If there is a better solution, let me know and I will update the answer accordingly.
Using the example above, the user filling out the form can choose their favorite color(s).
I should mention that the colors checkbox field was created in the CP, and all values for the checkbox are managed there. That way I can easily loop through the values within my template.
twig/html

    {% for option in craft.fields.getFieldByHandle('colors').getFieldType.options %}
        
            
                {{ option.label }}
           
        
   {% endfor %}
   {% if account is defined %}
       {{ errorList(account.getErrors('colors')) }}
   {% endif %}


---

