0

I have been looking for how to retrieve the inserted values when the submission is failed. Other than prepared fields (Name, Email, etc.), the inserted values disappear from the fields.

I already found this post, but those solutions did not work for me. P/T ContactForm Retrieving Custom fields Value after submitting form

My Craft is ver 3.7.50, Contact Form plugin is ver 2.5.1.

1 Answer 1

2

The contact form controller creates a Submission model with the submitted values and provides it to the template as a message variable. You should be able to access this variable inside your template like this:

{% set message = message ?? null %}

This message will contain the native properties (e.g. fromName, fromEmail):

{{ message.fromName }}
{{ message.fromEmail }}

If you use a single textarea field for the message body (name="message"), the value will be available as message.message:

{{ message.message }}

If you use multiple custom fields (message[body], message[phoneNumber]), message.message will be an array with those fields:

{{ message.message.body }}
{{ message.message.phoneNumber }}

One notable caveat is that if you're inside an included template that is included with the option with_context = false, the message variable will not be available. In this case, you have to go through the route manager to access it:

{% set routeParams = craft.app.getUrlManager().getRouteParams() %}
{% set message = routeParams.message ?? null %}

Note that this applies to contact form plugin version <=3.0.0. For 3.0.0 and above, the variable has been renamed to submission, see the changelog for details.

1
  • 1
    Thank you very much, it works as I wanted.
    – Kota.M
    Aug 3 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.