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
48 stars 61 forks source link

Payload missing after updating to Craft5 #1562

Closed FrDH closed 1 month ago

FrDH commented 1 month 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 1 month 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 1 month 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 1 month 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 month 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 1 month 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 1 month 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 1 month 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 } ) }}
FrDH commented 1 month ago

@tinusWCreators Thanks, but what you describe does not apply for me, it's a different issue.

tinusWCreators commented 1 month ago

@FrDH With the "value" key before the value, can you now see in the network tab that page is set to 'xx' while submitting? Or are there supposed to be some custom additional events in the background of your application?

FrDH commented 1 month ago

@tinusWCreators yes the values are visible in the payload in the network tab when I set these fields to be of type "hidden". But I don't think we are talking about the same issue here. In my case the entire form isn't submitted. This seems to be due to the freeform_payload that is missing.

FrDH commented 1 month ago

@kjmartens any thoughts maybe? I'm really having a hard time figuring this out.

A weird sidenote: When I try to add the demo formatting templates, I get this error:

Could not get demo template content. Please contact Solspace.

FrDH commented 1 month ago

I think I found the issue. We were passing through the entry.rootOwner to fetch some settings inside the formatting template:

{{ form.render({
   page: entry.rootOwner
})

This worked in Craft 4, but apparently not in Craft 5. The above template results in the "missing payload" error. I changed the template to pass through the entry ID instead of the entire entry instance, now (with the appropriate changes in the formatting template) everything is working:

{{ form.render({
   page: entry.rootOwner.id
})

Just FYI, I was following this example, maybe worth noting you can't pass through an entry instance. My guess it's due to how Craft now stores and fetches enties to/from the DB or something.

kjmartens commented 1 month ago

Thanks for letting us know @FrDH. Glad you were able to figure it out.

I'll have a look at the docs and update them as necessary. 🙂