How to use 'text' of Select field in Form as subject of email

@pamtbaau I like this solution you’ve provided in topic Help adding Contact form with multiple contact, it’s simple and easy.

But how do I get the text (not the value) of the selected option as subject?

I suppose:

  process:
    email:
      subject: '{{ form.value.topic|e }}'

@dapu, Well, all of the information needed to get the ‘text’ value is already available in the form definition:

---
form:                                   <---- the form
  name: contact
  fields:                               <---- the fields of the form
    topic:                              <---- field 'topic'
      type: select
      options:                          <---- array of options of field 'topic'
        'marketing@mysite.com': General information
        'sales@mysite.com': Sales information
  buttons:
    submit:
      type: submit
      value: Submit
  process:
    email:
      subject: 'Site Contact Form'
      body: "{% include 'forms/data.html.twig' %}"
      to: '{{ form.value.topic|e }}'     <---- get the value of field 'topic'
---

Combining the info:

  • To get the value of the Select field, use: form.value.topic.
    This will give you for example “sales@mysite.com
  • To get the array of options of the Select field, use: form.fields.topic.options
  • To get the text of the selection option use: form.fields.topic.options[form.value.topic]

Final result:

---
title: Typography
form:
  name: contact
  fields:
    topic:
      type: select
      options:
        'marketing@mysite.com': General information
        'sales@mysite.com': Sales information
  buttons:
    submit:
      type: submit
      value: Submit
  process:
    email:
      subject: '{{ form.fields.topic.options[form.value.topic] }}'
      body: "{% include 'forms/data.html.twig' %}"
      to: '{{ form.value.topic|e }}'
---
1 Like

Right… Value - not key :slight_smile:

@Karmalakas, On MDN about Select, I did not find unambiguous terms to denote the value of a <select> and value and innerText of <option>

Works perfectly! Thanks a lot. For the sake of completeness, I’ll add my completed form here:

form:
  fields:
    -
            name: subject
            label: Thema
            placeholder: 'Bitte wählen Sie ein Thema ...'
            type: select
            size: long
            classes: 'mb-3 form-control'
            validate:
                required: true
            options:
                mail1@mail.de: Allgemein
                mail2@mail.de: Probetraining
                mail3@mail.de: Sponsoring
 process:
        -
            email:
                from: '{{ config.plugins.email.from }}'
                to:
                    - '{{ form.value.subject|e }}'
                subject: 'Thema: {{ form.fields.subject.options[form.value.subject] }}'
                body: '{% include ''mail/contact.html.twig'' %}'
            display: /kontakt/danke
            captcha: true
        -
            message: Silence!

@dapu, “For the sake of completeness” does not seem to mean “Deutsche gründlichkeit” (German thoroughness)…

Your “complete” form looks incorrect and indeed throws an error…

Although your intention is appreciated, only correct code is helpful for the community.