I'm new to Twig, and I'm struggling to compare a value from a checkbox with a value from a drop-down

   {# sourceCountry - checkbox with a list of countries #}
   {# currentUser.country - dropdown with a list of countries #}

   {% for sourceCountry in region.targetCountries %}
      {{ sourceCountry }} = {{ currentUser.country }}   <br>
      {{ dump(sourceCountry == currentUser.country) }}
      {% if sourceCountry == currentUser.country %} 
         {% set visibility = true %}
      {% endif %}
   {% endfor %}

The result I'm getting from this is:



Most likely something to do with variable types, but no idea how to solve this. I've searched for some Twig 'to string' conversions, but no luck.

1 Answer 1


A checkboxes field returns an array of models, that you can inspect using {{ dump(sourceCountry) }} in your loop.

The model has a __toString method that outputs the checkbox value when you output the model using {{ sourceCountry }}. This magic method doesn’t do its thing when you use the model in a comparison operator, so you are currently comparing a model to a string.

Access the value property directly and your conditional should work.

{% if sourceCountry.value == 'GB' %} 
    {% set visibility = true %}
{% endif %}
  • I knew I'm missing something obvious, thanks Carl
    – Lukigi
    Commented Jan 12, 2017 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.