1

I'm trying to fetch entries from a channel via a front-end form featuring multiple filters - several dropdown selects and one checkbox field - kind of like an advanced search feature.

The form works OK when using the select dropdown fields but for some reason I can't get the checkbox input in the form to work. I have followed the example in the docs here.

This is what the checkbox part of the form looks like:

{# Checkboxes Based on a Checkbox Field #}
<label class="heading" for="features">Features</label>

{% for option in craft.fields.getFieldById(15).settings.options %}
    {% set checked = false %}

    {% if option.selected %}
        {% set checked = true %}
    {% endif %}

    {% if not checked %}
        <input type="hidden" name="fields[features][]" value="">
    {% endif %}

    <label>
        <input type="checkbox" name="fields[features][]" value="{{ option.value }}" {% if checked %} checked="checked"{% endif %}>{{ option.label }}
    </label>
{% endfor %}

This throws an error in the template:

Key "selected" for array with keys "label, value, default" does not exist

On the line above:

{% if option.selected %}

Further, on my search results template, I'm using getParam to pull the values from the query string like so:

{% set location = craft.request.getParam('location') %}
{% set realEstateType = craft.request.getParam('type') %}
{% set bedrooms = craft.request.getParam('bedrooms') %}
{% set priceRange = craft.request.getParam('price-range') %}

and then

{% set results = craft.entries.search().location(location).realEstateType(realEstateType).bedrooms(bedrooms).priceRange(priceRange) %}

This works OK without the offending Checkbox field. I suspect because there are possibly multiple values submitted for a checkbox field that it may not work - should I use search instead of getParam?

Sorry for the long-winded question, any help would be appreciated.

  • Any reason you're going through the field service (craft.fields.getFieldById(15)) to get to the checkbox data and not through the entry that the checkbox field is attached to? – Brad Bell Aug 22 '14 at 1:59
  • Well, the form is outside an entries loop so I thought this is the only way to access that data...? – Patrick Nesbitt Aug 22 '14 at 2:03
  • Yeah, just curious. Hope the answer helps. – Brad Bell Aug 22 '14 at 2:11
  • Can you share me your template file? I'm working on a same kind of page and can't figure out how to do it. Do you use categories in your filter? – Johannes Lamers Aug 26 '14 at 8:02
3

I came across a similar issue today where I was creating a search form that included checkboxes. I did it as follows.

First, I checked if the URL had the query string I was looking for and joined the query string since it's an array. I used OR because I want it to search for any of the checked options.

{% if craft.request.getParam('services') %} {% set services = craft.request.getParam('services') %} {% set services = services|join(' OR ') %} {% endif %}

The query string in getParam('services') is the same as the name field of the checkbox.

Then I listed out my options

{% for option in craft.fields.getFieldByHandle('memberServices').settings.options %} <li> <label class="form__label" for="{{ option.value }}"><input type="checkbox" name="services[]" id="{{ option.value }}" {% if services is defined and option.value in services %}checked{% endif %} value="{{ option.value }}" >{{ option.label }}</label> </li> {% endfor %}

The conditional that gives the checkbox the checked attribute is

{% if services is defined and option.value in services %}checked{% endif %}

That is, if services is defined (is in the URL) and the option value is contained within the services variable, apply the checked attribute.

It also works with multiple checkboxes, IE the URL search?q=&services%5B%5D=counselling&services%5B%5D=supervision&sessions=individual will check the "Counselling" and "Supervision" "services" checkboxes, and the "Individual" "sessions" checkbox.

1

craft.fields.getFieldById() will return a FieldModel and calling settings on that is going to return the actual field settings.

I believe you'll want to get something like this to get the actual Checkboxes field type (completely untested):

{% set checkboxFieldType = craft.fields.getFieldById(15).getFieldType() %}

{% for option in checkboxFieldType.options %}
    ....
{% endfor %}
  • No luck, it throws the same error. Incidentally, using craft.fields.getFieldById() for Dropdown fields submits the form fine and returns the correct entries in the search template. – Patrick Nesbitt Aug 22 '14 at 2: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.