1

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.

1 Answer 1

0

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

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.