Hi, I have a quick question regarding twig and forms: Let’s say my yaml is as below for a frontend form:
name: test
type: radio
options:
foo1: bar1
foo2: bar2
foo3: bar3
foo4: bar4
In the mail sent with data.html.twig, I can retrieve the ‘foo’ selected with {{ string(form.value(test)|e) }} but I would like to display instead the matching value of ‘bar’.
Any idea?
After the user hits the submit button a post / get happens and send the key | value pair to the server. In your case the key is “test” and the value is “foo1” / “foo2” / “foo3” or “foo4”. Depends on the option the user selected. Unfortunately this is all the data you could grap in the mail. Which I would try is to use hidden fields for sending the display values with the keys of your select list.
Thanks for your help! It might indeed be a solution, but I finally found, after hours of research how to achieve it. Leaving the code here in case it might help:
{% for field in form.fields %}
{% block field %}
<p>{% block field_label %}
<strong>{{ field.label }}</strong>
{% endblock %}:
{% block field_value %}
{% if field.type == 'radio' %}
{% set result = string(form.value(field.name)|e) %}
{{ field.options[result] }}
{% else %}
{{ string(form.value(field.name)|e)|nl2br }}
{% endif %}
{% endblock %}</p>
{% endblock %}
{% endfor %}