@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?
Community guidelines
Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.
Solved by pamtbaau View solution
@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'
'[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:
value of the Select field, use: form.value.topic.form.fields.topic.optionstext of the selection option use: form.fields.topic.options[form.value.topic]Final result:
---
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 }}'
---
@pamtbaau:
YAMLprocess: email: subject: '{{ form.fields.topic.options[form.value.topic] }}'
Right.. Value - not key 🙂
@Karmalakas, On MDN about Select, I did not find unambiguous terms to denote the value of a <select> and value and innerText of <option> ...
@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:
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!
@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.
Log in to reply.
| Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| 13 | 1140 | 4 months ago | ||
| 0 | 63 | 5 months ago | ||
| 9 | 138 | 6 months ago | ||
| 7 | 114 | 7 months ago | ||
| 10 | 132 | 7 months ago |