formie icon indicating copy to clipboard operation
formie copied to clipboard

Snapshot data won't save on controller action before session exists

Open mijewe opened this issue 1 year ago • 2 comments

Describe the bug

I've got a custom controller action that loads a template, loads a Formie form with it, and sets some custom snapshotData on the form.

$form->setSnapshotData('extraData', $extraData);

return $this->renderTemplate('template.twig', [ 'form' => $form ]);

If all cookies and session data are cleared, on the first page load the snapshot data will be empty. On the second page load, the extraData will have been set correctly.

This is because the setSnapshotData function checks Session::exists(), which returns false on this first page load.


My temporary low tech fix for this is to set the snapshot data in the template

{% do form.setSnapshotData('extraData', extraData) %}
{{ craft.formie.renderForm(form) }}

but this isn't ideal and wouldn't work for every use case.

Steps to reproduce

as above

Form settings

  • Multi-page form: No
  • Submission Method: Page reload
  • Client-side Validation: Yes
  • Custom Form Templates: No

Craft CMS version

4.9.0

Plugin version

2.1.13

Multi-site?

Yes

Additional context

No response

mijewe avatar May 02 '24 14:05 mijewe

Fixed for the next release. To get this early, run composer require verbb/formie:"dev-craft-4 as 2.1.13".

engram-design avatar May 03 '24 00:05 engram-design

Thanks!

FYI this also appears to have fixed a similar problem I was looking into, where my PHP-edited Webhook Integration wasn't firing on first load, presumably for the same reason as not having a session to save into.

mijewe avatar May 03 '24 11:05 mijewe

Updated in 2.1.14

engram-design avatar May 07 '24 15:05 engram-design