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.

Forms & Blueprints

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

Solved by pamtbaau View solution

Started by Danilo Puchelt 4 years ago · 6 replies · 521 views
4 years ago

I suppose:

TWIG
  process:
    email:
      subject: '{{ form.value.topic|e }}'
4 years ago Solution

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

YAML
---
form:                                   <---- the form
  name: contact
  fields:                               <---- the fields of the form
    topic:                              <---- field 'topic'
      type: select
      options:                          <---- array of options of field 'topic'
        '[email protected]': General information
        '[email protected]': 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 "[email protected]"
  • 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:

YAML
---
title: Typography
form:
  name: contact
  fields:
    topic:
      type: select
      options:
        '[email protected]': General information
        '[email protected]': 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
4 years ago

@pamtbaau:

YAML
  process:
    email:
      subject: '{{ form.fields.topic.options[form.value.topic] }}'

Right.. Value - not key 🙂

4 years ago

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

4 years ago

@pamtbaau:
{{ form.fields.topic.options[form.value.topic] }}

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

YAML
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:
                m[email protected]: Allgemein
                m[email protected]: Probetraining
                m[email protected]: 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!
4 years ago

@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.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1136 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 110 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 129 7 months ago