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.