CASE: access the chosen value from a dropdown

When creating a dropdown with options, we advise making use of the option_values attribute, which makes sure the actual value of the selected option remains the same, even when you change languages (this to avoid building logic per possible language).

A clear example would be a dropdown to chose a country:

{% comment %}Translation for dropdown{% endcomment %}
{% t= "t_country_options" default:"Belgium|Luxembourg|Netherland" nl:"België|Luxemburg|Nederland" fr:"Belgique|Luxembourg|Pays-Bas" %}

{% comment %}show dropdown{% endcomment %}
{% input custom.country_residence.country_code as:select options:"t_country_options" option_values:"BE|LU|NL" assign:chosen_country_code %}

However, the variable chosen_country_code will show the option value (e.g. “LU”), as this is the actual database value ( the actual value residing in custom.country_residence.country_code ).

But what if you’d like to show the selected value instead of the option value (so “Luxemburg” instead of “LU”)?
Well, good news, because as of now, it is possible to access just that, by using the assign_option attribute:

{% input custom.country_residence.country_code as:select options:"t_country_options" option_values:"BE|LU|NL" assign:chosen_country_code assign_option:selected_country_name %}

A simple example:

{% comment %}Translation for dropdown{% endcomment %}
{% t= "t_country_options" default:"Belgium|Luxembourg|Netherland" nl:"België|Luxemburg|Nederland" fr:"Belgique|Luxembourg|Pays-Bas" %}
{% t= "t_selected_residence" default:"The residence is located in {{ selected_country_name }}." %}

{% comment %}show dropdown{% endcomment %}
{% input custom.country_residence.country_code as:select options:"t_country_options" option_values:"BE|LU|NL" assign:chosen_country_code assign_option:selected_country_name %}

{% comment %}show sentence{% endcomment %}
{% t "t_selected_residence" selected_country_name:selected_country_name %}

with the following result:

1 Like