verbb/formie

Value for hidden fields from dynamic Cookie working inconsistently

Closed this issue · 2 comments

Describe the bug

Hi all,

In one of our projects, some cookies are set via JavaScript, depending on various URL parameters. The cookies are set correctly after the page has been loaded, which (in my opinion) causes the following issue:

When submitting a form and mapping hidden fields to those cookies, the fields in the submission sometimes appear empty and are missing its value. I would guess that this is due to some kind of race condition, with the formie scripts setting the values before our script sets the cookies. The thing is, we cannot control when our cookies will be set, since it is dependent on the user accepting the cookie consent, which can cause the cookie script to run pretty delayed compared to Formie's scripts.

I would guess that just checking on form submit and — if the cookies are present — setting the hidden fields' values would solve this issue.

Is this considered a bug or is the feature working as intended?

Steps to reproduce

  1. Create a form with hidden fields mapped to cookies
  2. Set cookies dynamically (and possibly very delayed) after the page has loaded

Form settings

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

Craft CMS version

Craft Pro 4.11.1

Plugin version

2.1.23

Multi-site?

Yes

Additional context

No response

Yeah, that's an interesting point, we probably should populate those values via JS on the onBeforeFormieSubmit.

There's some things to consider such as multi-page forms (should these cookies be added on every page submit, or just the final one?), but otherwise I'd say that's pretty straightforward.

For now, I might recommend bypassing the field setting for the cookie, and just doing the hidden field population purely in JS using the onBeforeFormieSubmit to act as you describe.

@engram-design Thanks for the quick reply. Yes, that's what I was thinking. Will work around it for now, using onBeforeFormieSubmit. Thank you!