Answers.Body: <p>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.</p>
  
  <pre><code>http://example.com/?dateFrom=&amp;dateUntil=&amp;country=&amp;priceMin=&amp;priceMax=
  </code></pre>
  
  <p>But you may have the need to directly link to <code>http://example.com/?country=France</code>, 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 <a href="http://twig.sensiolabs.org/doc/filters/default.html" rel="nofollow">the Twig default filter</a>.</p>
  
  <pre><code>{% set country = craft.request.getParam('country') | default(null) %}
  </code></pre>
  
  <p>The <code>null</code> 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 <code>options</code>, and then use the Twig merge filter to add more options based on conditions.</p>
  
  <pre><code>{% set options = {
    section: 'events',
    order: 'score'
  } %}
  
  {% if dateFrom %}
    {% set options = options | merge({ start: '&gt;=' ~ dateFrom }) %}
  {% endif %}
  
  {% set results = craft.entries(options) %}
  </code></pre>
  
  <p>Optionally, you can use the ternary operator shorthand syntax instead of <code>{% if dateFrom %}</code>. Whichever is easier.</p>
  
  <pre><code>`{% set options = dateFrom ? options | merge({ start: '&gt;=' ~ dateFrom }) : options %}`
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-04T06:11:59.777Z
Answers.LastActivityDate: 2016-04-04T06:11:59.777Z
Answers.UserDisplayName: Alex Roper
Body: <p>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.</p>
  
  <p>The desired functionality should be that none of the fields should be required. </p>
  
  <p>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.</p>
  
  <p>This is a stripped down version of my form:</p>
  
  <pre><code>`&lt;form action="{{ url('results') }}"&gt;
  
  &lt;input type="text" name="dateFrom"&gt; //this is a datepicker
  &lt;input type="text" name="dateUntil"&gt; //this is a datepicker
  
  &lt;select name="country"&gt;
    &lt;option&gt;&lt;/option&gt;
    &lt;option value="france"&gt;france&lt;/option&gt;
    &lt;option value="belgium"&gt;belgium&lt;/option&gt;
  &lt;/select&gt;
  
  &lt;input type="text" name="priceMin"&gt;
  &lt;input type="text" name="priceMax"&gt;
  
  &lt;input type="submit" value="Go"&gt;
  &lt;/form&gt;`
  </code></pre>
  
  <p>And here's what i've got on my results page</p>
  
  <pre><code>`{% 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: '&gt;=' ~ dateFrom,
    end: '&lt;=' ~ dateUntil,
    country: country,
    price: [
      'and',
      '&gt;=' ~ priceMin,
      '&lt;=' ~ priceMax,
      ]
  }) %}
  
  
  {% if results|length %}
  &lt;p&gt;{{ results|length }} results:&lt;/p&gt;
  &lt;ul&gt;
    {% for entry in results %}
    &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }} - {{ entry. price }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
  {% else %}
  &lt;p&gt;Your search didn’t return any results.&lt;/p&gt;
  {% endif %}`
  </code></pre>
  
  <p>Anyone have any suggestions?
  Thank you!</p>
  
Tags: <form><datetime>
Title: How can I build a combined search without making all fields required?
ThreadText: # How can I build a combined search without making all fields required?
  
  (Asked by: username_0 on 2016-04-03)
  
  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></option>
    <option value="france">france</option>
    <option value="belgium">belgium</option>
  </select>
  
  <input type="text" name="priceMin">
  <input type="text" name="priceMax">
  
  <input type="submit" value="Go">
  </form>`
  
  ```
  
  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: [
      'and',
      '>=' ~ priceMin,
      '<=' ~ priceMax,
      ]
  }) %}
  
  {% if results|length %}
  <p>{{ results|length }} results:</p>
  <ul>
    {% for entry in results %}
    <li><a href="{{ entry.url }}">{{ entry.title }} - {{ entry. price }}</a></li>
    {% endfor %}
  </ul>
  {% else %}
  <p>Your search didn’t return any results.</p>
  {% endif %}`
  
  ```
  
  Anyone have any suggestions?
  Thank you!
  
  **Tags:** form, datetime
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-04. Score: 1 | Accepted answer
  
  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.
  
  ```
  http://example.com/?dateFrom=&dateUntil=&country=&priceMin=&priceMax=
  
  ```
  
  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](http://twig.sensiolabs.org/doc/filters/default.html).
  
  ```
  {% 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 %}`
  
  ```
  
  
  ---
  
  