<p>If you want a Dropdown field with the option to <em>not</em> select a value (or to clear out the current selection), you can just add an option (typically the first) with an empty <code>value</code> column. If the empty value/option is selected, the following conditional will evaluate to <code>false</code>, and if any other, non-empty value is selected it will evaluate to <code>true</code>:</p>

<pre><code>{% if entry.dropdownFieldHandle %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% else %}
    I have no value in the dropdown
{% endif %}
</code></pre>

<p>Note that for Dropdown fields its also possible to leave the <code>label</code> column empty, or to use something like "Pick a value..." as a placeholder.</p>

<p>If you use <code>null</code> instead of an empty value, the above conditional will always evaluate to <code>true</code>, because Twig will treat the value <code>null</code> as a string (i.e. <code>"null"</code>), not an actual <code>NULL</code> value.</p>

<p>As for the <code>|length</code> filter, since your question brings it up – the only thing <code>|length</code> does is to return the <em>length</em> of whatever variable its suffixed to. For an iterable object (like an array or a relational field like Assets or Entries) the length will be the amount of elements in that object (for instance, the number of Assets selected in an Assets field); for strings and numbers it will be the number of characters (meaning <code>"foo"|length</code> and <code>123|length</code> will both return <code>3</code>).</p>

<p>The reason <code>|length</code> is often used in conditionals, is that <code>0</code> is considered a "falsey" value, and any length larger than <code>0</code> is considered a "truthy" value. In other words, a conditional like <code>{% if ""|length %}</code> will be evaluated to <code>false</code>, because <code>""</code> is an empty string and hence will return <code>0</code> characters, while a conditional like <code>{% if "foo"|length %}</code> will evaluate to <code>true</code> because the <code>length</code> filter will return <code>3</code> characters and <code>3</code> is a "truthy" value.</p>

<p>That being said, the reason why the <code>|length</code> filter isn't neccessary in the above example conditional is simply because Twig will automatically evaluate conditionals testing empty strings to <code>false</code>. So while <code>{% if entry.dropdownFieldHandle|length %}</code> would also work (i.e. evaluate to <code>false</code> for empty values), it isn't actually neccessary.</p>

<p>Finally, <code>is not</code> (or simply <code>not</code>) is simply Twig-speak for the <code>!=</code> operator, i.e. <code>is not equal to</code>. So if you wanted to reverse the conditional above, here's how that would look:</p>

<pre><code>{% if not entry.dropdownFieldHandle %}
    I have no value in the dropdown
{% else %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-12-21T03:10:12.923Z
I changed the first value to blank (no data) and tried:

{% if entry.GamesGraphicsMode %}Graphics Mode: {{ entry.GamesGraphicsMode }}{% endif %}

But the result is that 'Graphics Mode:' is shown regardless of whether I have the drop-down selected to the first option (blank) or an option with data.
DevServe
CC BY-SA 3.0
2016-12-21T06:52:43.663Z
Is the option value really empty, or did you accidentally make it a blank space or something like that? Also, note that Dropdown fields are not dynamic – i.e. if you change the `GamesGraphicsMode` field's options (e.g. by changing an option value from `"null"` to an empty value), any existing values for your entries won't update; you'll need to resave existing entries. You can always try to `{{ dump(entry. GamesGraphicsMode) }}` and see what it contains, because I guarantee that it isn't empty :)  (the `dump` function requires [`devMode`](https://craftcms.com/support/dev-mode) to be on)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-12-22T00:38:23.897Z
I got it to work. I set the first Label to "Select" and option to "select", in my template, I used: {% if entry.GamesGraphicsMode != "select" %}
DevServe
CC BY-SA 3.0
2016-12-22T08:22:39.663Z
Sure, that'll work. I'm still pretty sure there was some junk data (a blank space, invisible characters, etc) in your previous "empty" value cell though, because leaving the value empty and simply doing `{% if entry.GamesGraphicsMode %}` should work, too (I've done it like that many times). Anyway, it's that you got it working :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-12-20T14:05:01.460Z
2016-12-20T22:47:23.233Z
2016-12-20T22:47:23.233Z
Mats Mikkel Rummelhoff
<p>I have read docs on Craft CMS site and Googled some related articles but can't seem to get my head around checking for conditions for a field.</p>

<p>For example, if I have a 'Fruit' field drop-down:
Apple
Banana
Orange</p>

<p>Should I add a 4th value with 'null'? So in my template, I want to display the field if it is Apple, Banana or Orange but not if null.</p>

<p>I get confused when to to use '|length'  or 'is empty' or 'is not' conditionals.</p>

<p>In another example, I tried to setup a drop-down for a field called 'Rating'
The values were 1, 2, 3, 4, 5
I couldn't get the conditional to work, so I thought to make the field a number type. And then I tried a conditional 'rating > 0' but also didn't succeed.</p>

<templating><conditional><entry-type>
Checking if a field contains information
# Checking if a field contains information

(Asked by: username_0 on 2016-12-20)

I have read docs on Craft CMS site and Googled some related articles but can't seem to get my head around checking for conditions for a field.

For example, if I have a 'Fruit' field drop-down:
Apple
Banana
Orange

Should I add a 4th value with 'null'? So in my template, I want to display the field if it is Apple, Banana or Orange but not if null.

I get confused when to to use '|length' or 'is empty' or 'is not' conditionals.

In another example, I tried to setup a drop-down for a field called 'Rating'
The values were 1, 2, 3, 4, 5
I couldn't get the conditional to work, so I thought to make the field a number type. And then I tried a conditional 'rating > 0' but also didn't succeed.

**Tags:** templating, conditional, entry-type


---

## 1 answers

### Answer by username_1 on 2016-12-20. Score: 3 | Accepted answer

If you want a Dropdown field with the option to not select a value (or to clear out the current selection), you can just add an option (typically the first) with an empty value column. If the empty value/option is selected, the following conditional will evaluate to false, and if any other, non-empty value is selected it will evaluate to true:
{% if entry.dropdownFieldHandle %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% else %}
    I have no value in the dropdown
{% endif %}

Note that for Dropdown fields its also possible to leave the label column empty, or to use something like "Pick a value..." as a placeholder.
If you use null instead of an empty value, the above conditional will always evaluate to true, because Twig will treat the value null as a string (i.e. "null"), not an actual NULL value.
As for the |length filter, since your question brings it up – the only thing |length does is to return the length of whatever variable its suffixed to. For an iterable object (like an array or a relational field like Assets or Entries) the length will be the amount of elements in that object (for instance, the number of Assets selected in an Assets field); for strings and numbers it will be the number of characters (meaning "foo"|length and 123|length will both return 3).
The reason |length is often used in conditionals, is that 0 is considered a "falsey" value, and any length larger than 0 is considered a "truthy" value. In other words, a conditional like {% if ""|length %} will be evaluated to false, because "" is an empty string and hence will return 0 characters, while a conditional like {% if "foo"|length %} will evaluate to true because the length filter will return 3 characters and 3 is a "truthy" value.
That being said, the reason why the |length filter isn't neccessary in the above example conditional is simply because Twig will automatically evaluate conditionals testing empty strings to false. So while {% if entry.dropdownFieldHandle|length %} would also work (i.e. evaluate to false for empty values), it isn't actually neccessary.
Finally, is not (or simply not) is simply Twig-speak for the != operator, i.e. is not equal to. So if you wanted to reverse the conditional above, here's how that would look:
{% if not entry.dropdownFieldHandle %}
    I have no value in the dropdown
{% else %}
    I have a value: {{ entry.dropdownFieldHandle }}
{% endif %}

> **Comments:**
> **username_0**: I changed the first value to blank (no data) and tried:  {% if entry.GamesGraphicsMode %}Graphics Mode: {{ entry.GamesGraphicsMode }}{% endif %}  But the result is that 'Graphics Mode:' is shown regardless of whether I have the drop-down selected to the first option (blank) or an option with data.  
> **username_1**: Is the option value really empty, or did you accidentally make it a blank space or something like that? Also, note that Dropdown fields are not dynamic – i.e. if you change the `GamesGraphicsMode` field's options (e.g. by changing an option value from `"null"` to an empty value), any existing values for your entries won't update; you'll need to resave existing entries. You can always try to `{{ dump(entry. GamesGraphicsMode) }}` and see what it contains, because I guarantee that it isn't empty :)  (the `dump` function requires [`devMode`](https://craftcms.com/support/dev-mode) to be on)  
> **username_0**: I got it to work. I set the first Label to "Select" and option to "select", in my template, I used: {% if entry.GamesGraphicsMode != "select" %}  
> **username_1**: Sure, that'll work. I'm still pretty sure there was some junk data (a blank space, invisible characters, etc) in your previous "empty" value cell though, because leaving the value empty and simply doing `{% if entry.GamesGraphicsMode %}` should work, too (I've done it like that many times). Anyway, it's that you got it working :)  

---

