'Select 'Please Select' if var does not match value in dropdown
I am new to Twig and struggling with the following scenario.
I have a select for country names (United Kingdom & Republic of Ireland) and I'm struggling when the Twig variable value returned does not match a value in the select.
I have it working if the variable is empty but need help with the above.
This is my full select HTML (I have removed all the Twig code to make it clear)
<select id="compAddCountryDd" name="compAddCountryDd" class="form-control address">
<option value='' class="optionPlaceholder">Please Select</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Republic of Ireland">Republic of Ireland</option>
</select>
This is the code I have for my 'Please Select' option if empty, which I have working
{% if companyDetails is defined and companyDetails.address.country == '' %}
<option value='' class="optionPlaceholder"
selected style="display: none" disabled>
Please Select
</option>
{% endif %}
But I can't seem to get it to display if the variable is defined but does not match the values in the select.
This I have tried the following code for the Twig if
{% if companyDetails is defined and companyDetails.address.country == '' and
companyDetails.address.country != 'United Kingdom' and
companyDetails.address.country != 'Republic of Ireland' %}
and also
{% if companyDetails is defined and companyDetails.address.country == '' or
companyDetails.address.country != 'United Kingdom' or
companyDetails.address.country != 'Republic of Ireland' %}
Basically, I need my 'Please Select' option to be displayed if it:
- Is defined
- Does not equal 'United Kingdom'
- Does not equal 'Republic of Ireland'
Solution 1:[1]
You are missing some parentheses here to comply to your logic:
- You want to the variable to be defined
- AND
- The country is either empty OR not equal to the UK AND not equal to Ireland
{% if companyDetails|default and (companyDetails.address.country|trim == '' or companyDetails.address.country != 'United Kingdom' and companyDetails.address.country != 'Republic of Ireland') %}
Show
{% else %}
Don't show
{% endif %}
I would however rewrite this to the following:
{% if companyDetails|default and (companyDetails.address.country|trim == '' or companyDetails.address.country not in [ 'United Kingdom', 'Republic of Ireland' ]) %}
Show
{% else %}
Don't show
{% endif %}
Solution 2:[2]
It will be a good opportunity to create a form class with a ChoiceType ? Easier to manage in your controller (get repository, show/not show/disabled), to separate code and to add translations
Some good examples in the doc : https://symfony.com/doc/current/reference/forms/types/choice.html
Another one good example : https://symfonycasts.com/screencast/symfony-forms/dynamic-choice-type
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |

