Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Matching form value in sent email

Started by Muut Archive 10 years ago · 2 replies · 561 views
10 years ago

Hi, I have a quick question regarding twig and forms: Let's say my yaml is as below for a frontend form:

YAML
            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?

10 years ago

This is an standard HTML Form behavior. Grav renders your example to:

TXT
<select name="test">
<option value="foo1">bar1</option>
<option value="foo2">bar2</option>
<option value="foo3">bar3</option>
<option value="foo4">bar4</option>
</select>

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.

YAML
header.foo1:
  type: hidden
  default: bar1
header.foo2:
  type: hidden
  default: bar2
header.foo2:
  type: hidden
  default: bar2

Then you could resolve the display value over Twig like so: (untested)

TXT

form.value(form.value(test))|e
---
10 years ago

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:

TWIG
  {% 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 %} 

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1281 9 years ago
Archive · by Muut Archive, 9 years ago
2 888 9 years ago
Archive · by Muut Archive, 9 years ago
2 4018 9 years ago
Archive · by Muut Archive, 9 years ago
1 2894 9 years ago
Archive · by Muut Archive, 9 years ago
3 1077 9 years ago