0

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:

<h1>Search Results</h1>

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

  • @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) – Rob Jan 16 '16 at 14:44
  • How many checkboxes / categories do you have, @Rob? Have you considered using lightswitches? – carlcs Jan 16 '16 at 14:57
  • Here's an example for a dynamic relatedTo parameter: Dynamic relatedTo queries (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 Jan 16 '16 at 15:02
  • @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 Jan 16 '16 at 15:08
  • 1
    Just released it: github.com/carlcs/craft-checkboxfield – carlcs Jan 16 '16 at 17:33