I'm trying to build a form where users can search for events based on a date range, location and price range. I've got the form working, but having a bit of trouble when some fields/ selects are left blank.

The desired functionality should be that none of the fields should be required.

For example: If I select a location, I would like to see all events at that location even if the dates and price ranges are left blank. Or if all fields are left blank, I should see all events.

This is a stripped down version of my form:

`<form action="{{ url('results') }}">

<input type="text" name="dateFrom"> //this is a datepicker
<input type="text" name="dateUntil"> //this is a datepicker

<select name="country">
  <option value="france">france</option>
  <option value="belgium">belgium</option>

<input type="text" name="priceMin">
<input type="text" name="priceMax">

<input type="submit" value="Go">

And here's what i've got on my results page

`{% set country = craft.request.getParam('country') %}
{% set dateFrom = craft.request.getParam('dateFrom')|date("Y-m-d") %}
{% set dateUntil = craft.request.getParam('dateUntil')|date_modify("+1 day")|date("Y-m-d") %}
{% set priceMin = craft.request.getParam('priceMin')  %}
{% set priceMax = craft.request.getParam('priceMax') %}

{% set results = craft.entries({
  section: 'events',
  order: 'score',
  start: '>=' ~ dateFrom,
  end: '<=' ~ dateUntil,
  country: country,
  price: [
    '>=' ~ priceMin,
    '<=' ~ priceMax,
}) %}

{% if results|length %}
<p>{{ results|length }} results:</p>
  {% for entry in results %}
  <li><a href="{{ entry.url }}">{{ entry.title }} - {{ entry. price }}</a></li>
  {% endfor %}
{% else %}
<p>Your search didn’t return any results.</p>
{% endif %}`

Anyone have any suggestions? Thank you!


When you submit the form you have above, it's already going to have those parameters in the URL even if they have no form values selected.


But you may have the need to directly link to http://example.com/?country=France, or empty parameters may cause errors in your template. If that's the case you probably need to define default values for missing and empty parameters. The easiest way to do this is with the Twig default filter.

{% set country = craft.request.getParam('country') | default(null) %}

The null value should work as a default for some parameters. If there are other parameters you can't easily create a default for or you only want to use parameters if they're defined, you could create an array of your default options, and then use the Twig merge filter to add more options based on conditions.

{% set options = {
  section: 'events',
  order: 'score'
} %}

{% if dateFrom %}
  {% set options = options | merge({ start: '>=' ~ dateFrom }) %}
{% endif %}

{% set results = craft.entries(options) %}

Optionally, you can use the ternary operator shorthand syntax instead of {% if dateFrom %}. Whichever is easier.

`{% set options = dateFrom ? options | merge({ start: '>=' ~ dateFrom }) : options %}`

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.