<p>So it seems that <a href="https://craftcms.stackexchange.com/questions/13099/advanced-filtered-search-matrix-fields-and-arrays">Matrix fields don't work with the object syntax</a> so I've changed my field structure to use standard dropdowns, checkboxes and a number field.</p>

<p>I have the dropdown (<code>gender</code>) and number field (<code>maxPrice</code>) working fine, but I can't seem to figure out what to enter for the checkboxes. I assumed it'd be in a similar format to what I've seen elsewhere, as you can see below:</p>

<pre><code>&lt;h1&gt;Search Results&lt;/h1&gt;

{% set tags = craft.request.getParam('tags') %}

{# Gender is a dropdown, so it's a single value or no value
 # - If an empty value is selected, it returns both genders #}
{% set gender = craft.request.getParam('gender') %}

{% set maxPrice = craft.request.getParam('maxPrice') %}
{% set maxPrice = maxPrice is not empty ? maxPrice : '99' %}

{% set sessions = craft.request.getParam('sessions') %}
{% set sessions = sessions is not empty ? sessions : ['skype', 'chat', 'phone', 'email'] %}
{% set sessions = ['and']|merge(sessions) %}

{% set hours = craft.request.getParam('hours') %}
{% set hours = hours is not empty ? hours : ['office', 'overtime', 'weekends'] %}
{% set hours = ['and']|merge(hours) %}

{% set results = craft.users({
  group: 'verified',
  search: "tagsSpecialismSearchable:#{tags}",
  gender: gender,
  profilePricingSession: [
    'and',
    '&gt; 0',
    "&lt;= #{maxPrice}"
  ],
  profileSessions: sessions,
  profileHours: hours,
}) %}

{% set users = shuffle(results) %}
</code></pre>

<p>Everything works except <code>profileSessions</code> and <code>profileHours</code>, which are both checkboxes. I assumed it might be that they need to be passed a <code>key: value</code> pair, i.e: <code>skype: true</code> or some other method, but nothing I've tried so far is working.</p>

<p>I happened on a post saying <a href="https://craftcms.stackexchange.com/questions/13190/search-against-a-checkboxes-fields-options">checkboxes store in a query unfriendly way</a>, so perhaps I'm better off changing the field again for something more friendly. They'd need to be multi-select of course, so categories would be the best bet if what I'm doing isn't possible.</p>

<p>Suggestions?</p>

CC BY-SA 3.0
2016-01-16T14:44:31.457Z
@carlcs Not quite, but if the sentiments are the same: i.e. checkboxes aren't searchable in this way, I guess I'll have to go with categories. If possible, could you point me in the direction of a post that uses categories with the object syntax in a similar way? Would I use `relatedTo`? (it still confuses me a bit, especially when using [`field`, `sourceElement` and `targetElement`](https://craftcms.com/docs/relations#the-relatedTo-param))
Rob
CC BY-SA 3.0
2016-01-16T14:57:39.560Z
How many checkboxes / categories do you have, @Rob? Have you considered using lightswitches?
carlcs
CC BY-SA 3.0
2016-01-16T15:02:43.237Z
Here's an example for a dynamic `relatedTo` parameter: [Dynamic relatedTo queries](http://craftcms.stackexchange.com/q/11870/125) (categories are most likely to be the `targetElement`, as they are the elements selected in the field and not the element the field is attachted to).
carlcs
CC BY-SA 3.0
2016-01-16T15:08:33.260Z
@carlcs Yeah that's the thing, I only have about 8 in total which seems overkill to use categories, but lightswitches might look a bit fuggly on the backend (the users can edit their profile in the CP). Is that a similar method to `dropdown: 'value'` or would it be something like `field: true` or `field: '0'`? Craft is brilliant but these little intricacies can take some getting used to — hopefully the v3 api will give us more options :)
Rob
CC BY-SA 3.0
2016-01-16T15:12:57.403Z
I have written a checkbox fieldtype (note the singular) for a project, which stores the data like a lightswitch field, but looks like the checkboxes field in the CP. I'll push it to GitHub for you, so you can keep the looks in the CP, but store in a query friendly way. ;-)
carlcs
CC BY-SA 3.0
2016-01-16T17:08:37.000Z
@carlcs Sounds great! Be good to see how you manage it .. Is it future compatable? So many ways to build a thing on Craft :)
Rob
CC BY-SA 3.0
2016-01-16T17:33:27.120Z
Just released it: https://github.com/carlcs/craft-checkboxfield
carlcs
CC BY-SA 3.0
2016-01-18T15:58:47.433Z
@carlcs Perfect, looks like it'll come in handy ... thanks!
Rob
CC BY-SA 3.0
2016-01-19T21:46:50.893Z
@carlcs I'm struggling to get your [Dynamic relatedTo query](http://craftcms.stackexchange.com/questions/11870/dynamic-relatedto-queries) example working, if using for multi-selectable categories in 2 category groups. I think [this example](http://craftcms.stackexchange.com/questions/9081/how-to-filter-entries-by-clicking-multiple-categories-on-front-end/9083#9083) is closer to what I'm looking for. Am I right in thinking the only way to do this is adding a `for loop` through all the `query[]=` group arrays? I don't think I fully understand what your example is doing.
Rob
CC BY-SA 3.0
2016-01-19T22:01:19.937Z
FYI I'm using slugs as values for the checkboxes. I assumed at first what you were doing is passing `query.art` parameter array into `.id()` and merging each with it's own `{ targetElement: categoryName }`, but I thought `.id()` and `.slug` could only take one string at a time. Is it something similar to the [second example in Twig docs](http://twig.sensiolabs.org/doc/filters/merge.html) which (due to being wrapped in `[]` creates a list of `key: value` pairs? Or am I off the mark? I come from a front-end background so this stuff makes my head hurt!
Rob
CC BY-SA 3.0
2016-01-19T23:30:48.870Z
@badlydrawnrob on Slack ... I almost have it figured out thanks to [Twig dump](http://twig.sensiolabs.org/doc/functions/dump.html) but the search results are doing some funky things.
Rob
<categories><search><queries><checkboxes>
Advanced search form with checkboxes
# Advanced search form with checkboxes

(Asked by: username_0 on 2016-01-15)

So it seems that Matrix fields don't work with the object syntax so I've changed my field structure to use standard dropdowns, checkboxes and a number field.
I have the dropdown (gender) and number field (maxPrice) working fine, but I can't seem to figure out what to enter for the checkboxes. I assumed it'd be in a similar format to what I've seen elsewhere, as you can see below:
Search Results

{% set tags = craft.request.getParam('tags') %}

{# Gender is a dropdown, so it's a single value or no value
 # - If an empty value is selected, it returns both genders #}
{% set gender = craft.request.getParam('gender') %}

{% set maxPrice = craft.request.getParam('maxPrice') %}
{% set maxPrice = maxPrice is not empty ? maxPrice : '99' %}

{% set sessions = craft.request.getParam('sessions') %}
{% set sessions = sessions is not empty ? sessions : ['skype', 'chat', 'phone', 'email'] %}
{% set sessions = ['and']|merge(sessions) %}

{% set hours = craft.request.getParam('hours') %}
{% set hours = hours is not empty ? hours : ['office', 'overtime', 'weekends'] %}
{% set hours = ['and']|merge(hours) %}

{% set results = craft.users({
  group: 'verified',
  search: "tagsSpecialismSearchable:#{tags}",
  gender: gender,
  profilePricingSession: [
    'and',
    '> 0',
    "<= #{maxPrice}"
  ],
  profileSessions: sessions,
  profileHours: hours,
}) %}

{% set users = shuffle(results) %}

Everything works except profileSessions and profileHours, which are both checkboxes. I assumed it might be that they need to be passed a key: value pair, i.e: skype: true or some other method, but nothing I've tried so far is working.
I happened on a post saying checkboxes store in a query unfriendly way, so perhaps I'm better off changing the field again for something more friendly. They'd need to be multi-select of course, so categories would be the best bet if what I'm doing isn't possible.
Suggestions?

**Tags:** categories, search, queries, checkboxes

> **Comments:**
> **username_0**: @username_1 Not quite, but if the sentiments are the same: i.e. checkboxes aren't searchable in this way, I guess I'll have to go with categories. If possible, could you point me in the direction of a post that uses categories with the object syntax in a similar way? Would I use `relatedTo`? (it still confuses me a bit, especially when using [`field`, `sourceElement` and `targetElement`](https://craftcms.com/docs/relations#the-relatedTo-param))  
> **username_1**: How many checkboxes / categories do you have, @username_0? Have you considered using lightswitches?  
> **username_1**: Here's an example for a dynamic `relatedTo` parameter: [Dynamic relatedTo queries](http://craftcms.stackexchange.com/q/11870/125) (categories are most likely to be the `targetElement`, as they are the elements selected in the field and not the element the field is attachted to).  
> **username_0**: @username_1 Yeah that's the thing, I only have about 8 in total which seems overkill to use categories, but lightswitches might look a bit fuggly on the backend (the users can edit their profile in the CP). Is that a similar method to `dropdown: 'value'` or would it be something like `field: true` or `field: '0'`? Craft is brilliant but these little intricacies can take some getting used to — hopefully the v3 api will give us more options :)  
> **username_1**: I have written a checkbox fieldtype (note the singular) for a project, which stores the data like a lightswitch field, but looks like the checkboxes field in the CP. I'll push it to GitHub for you, so you can keep the looks in the CP, but store in a query friendly way. ;-)  
> **username_0**: @username_1 Sounds great! Be good to see how you manage it .. Is it future compatable? So many ways to build a thing on Craft :)  
> **username_1**: Just released it: https://github.com/username_1/craft-checkboxfield  
> **username_0**: @username_1 Perfect, looks like it'll come in handy ... thanks!  
> **username_0**: @username_1 I'm struggling to get your [Dynamic relatedTo query](http://craftcms.stackexchange.com/questions/11870/dynamic-relatedto-queries) example working, if using for multi-selectable categories in 2 category groups. I think [this example](http://craftcms.stackexchange.com/questions/9081/how-to-filter-entries-by-clicking-multiple-categories-on-front-end/9083#9083) is closer to what I'm looking for. Am I right in thinking the only way to do this is adding a `for loop` through all the `query[]=` group arrays? I don't think I fully understand what your example is doing.  
> **username_0**: FYI I'm using slugs as values for the checkboxes. I assumed at first what you were doing is passing `query.art` parameter array into `.id()` and merging each with it's own `{ targetElement: categoryName }`, but I thought `.id()` and `.slug` could only take one string at a time. Is it something similar to the [second example in Twig docs](http://twig.sensiolabs.org/doc/filters/merge.html) which (due to being wrapped in `[]` creates a list of `key: value` pairs? Or am I off the mark? I come from a front-end background so this stuff makes my head hurt!  
> **username_0**: @badlydrawnrob on Slack ... I almost have it figured out thanks to [Twig dump](http://twig.sensiolabs.org/doc/functions/dump.html) but the search results are doing some funky things.  

---

## 0 answers

