solspace / craft-freeform

Freeform for Craft: The most reliable form builder that's ready for wherever your project takes you.
https://docs.solspace.com/craft/freeform/v5/
Other
47 stars 60 forks source link

Payload missing after updating to Craft5 #1562

Open FrDH opened 6 days ago

FrDH commented 6 days ago

What happened?

I recently updated our starter project to Craft5 and noticed the forms no longer can be submitted. Submitting the form returns the following error-messages:

This is both local (using DDEV) and on the staging server.

On first opening the page I can see a hidden input "freeform_payload" with an encoded value. After submitting and returning to the form with the error-messages, the value is different.

Errors and Stack Trace (if available)

- Payload missing
- Form has expired

How can we reproduce this?

  1. It worked on Craft4
  2. Updated to Craft5
  3. No longer works Sorry I don't know how else to describe this, any help would be much appreciated

Freeform Edition

Pro

Freeform Version

5.6.4

Craft Version

5.4.6

When did this issue start?

After upgrading from older Craft version

Previous Freeform Version

No response

kjmartens commented 3 days ago

Sorry for the trouble you're experiencing @FrDH.

Can you provide me with a bit more information about your setup? 🙂

  1. A screenshot of your Freeform Diagnostics page
  2. How are you rendering the form? Regularly with Twig, or is this a headless setup, etc?
  3. Are you caching the form/page? And if so, which method are you using and how are you resetting any tokens (code examples)?

Thanks!

FrDH commented 3 days ago

Thank you for the reply.

  1. Here's a screenshot of the diagnostics page. Scherm­afbeelding 2024-10-15 om 09 33 55

  2. We're rendering the for with Twig, a modified version of the "Grid" example

  3. Not that I know of. How could I check this? For the staging environment I had to turn off some of the redis, cache and session config (in app.php) because it still needs to be tweeked for Craft5. For local development (in DDEV) we had none of that config to begin with.

In the Craft4 starter project, Freeform remains to work as before. So my initial thought was some changed config or something. I realize this is probably an issue on our side, but any help is much appreciated.

FrDH commented 3 days ago

Would it make any difference if we de-install Freeform, clear the DB from all freeform tables and re-install Freeform?

tinusWCreators commented 1 day ago

I had a similar issue, Is the payload a hidden field or invissible, If it is an invissible field, could you try to set it to hidden. This did the trick for me. Also there is a change in code. You have to set fields: and use 'value'.


{% set overrideValues = [] %}
{% set overrideValues = {
        'currentPage': (page != null and page.title != null) ? { "value": page.title },
        'currentPageId': { "value": (page != null ? page.id ) },
        'currentPageUrl': page != null and page.url != null ?  { "value": page.url },
} %}

{{ form.render({ fieldIdPrefix: containerId, fields: overrideValues } ) }}

Also are you using caching like blitz cache? In that case you have to exclude a dynamic caching script let's say: templates/dynamic/freeform-token.twig And in this script you also have to add the payload. In my case:

{% set page = craft.app.elements.elementById(craft.app.request.get('page')) %}
{% set overrideValues = {
    'currentPage': (page != null and page.title != null) ? { "value": page.title },
    'currentPageId': { "value": craft.app.request.get('page') },
    'currentPageUrl': page != null and page.url != null ?  { "value": page.url },
} %}

{% set form = craft.freeform.form(craft.app.request.get('form'), { "fields": overrideValues }) %}
{% do form.registerContext %}
{{ {
    hash: form.hash,
    payload: form.payload,
    csrf: {
        name: craft.app.config.general.csrfTokenName,
        value: craft.app.request.csrfToken,
    }
}|json_encode|raw }}

See https://docs.solspace.com/craft/freeform/v5/guides/templating/caching-forms for the javascript to update the token an payload when using cache:

FrDH commented 2 hours ago

@tinusWCreators Thank you for the response. We set the overrideValues like this:

{% set values = [
   page: 'xx'
] %}
{{ form.render({ fieldIdPrefix: containerId, values } ) }}

Which worked fine in Freeform5 on Craft4, is there any reason this wouldn't work (and cause the "missing payload" error) in Freeform5 on Craft5 ? I changed it to your way but that didn't make any difference.

We don't use caching but if we did, wouldn't we have the same issue in Craft4 ? Just to be sure I added the {% do form.registerContext %} to several places in the template (both before and after rendering the form) to no avail.

Thanks again for taking the time.

FrDH commented 2 hours ago

@tinusWCreators What do you mean by "Is the payload a hidden field or invissible"? It is a field that is automatically added to the page, I don't think I can edit it in either the CP nor the templates, or can I?

tinusWCreators commented 43 minutes ago

@FrDH I was refuring to the following issue: https://github.com/solspace/craft-freeform/issues/1570 In this issue the "Page" field would be of the type invisible in the CMS, and might therefor not be set.

Could you try again with something like

{% set values = [
   page: { "value": "xx" },
] %}
{{ form.render({ fieldIdPrefix: "example-in-case-of-multiple-similar-forms-1", values } ) }}