question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to modify formData on turbo:submit-start

See original GitHub issue

I want to modify any form of my page when they are submitted. I tried to add some data to the form modifying the formData of the formSubmission object:

document.addEventListener('turbo:submit-start', function (event) {
    event.detail.formSubmission.formData.append('input_name', 'example_value');
});

but looks like the fetchRequest object that has the formData that will be submitted is already constructed, so changes in formSubmission object will not change the fetchRequest object. Am I right? How can I achieve to modify the form data via turbo event?

Thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
davidjr82commented, Jul 8, 2021

So finally I figure out how to make it works. Thanks @michelson for the idea of request.js. At the end, I have modified the fetchRequest body object of the formSubmission object. Just in case it helps someone in the future, here is how I did it:

document.addEventListener('turbo:submit-start', function (event) {
    event.detail.formSubmission.fetchRequest.body.append('input_name', 'example_value');
});

Thanks again!

0reactions
mike1o1commented, Jul 28, 2021

For anybody else coming to this, I was looking for a way to sync some fields on submit. Hooking into the turbo:submit-start seemed like the ideal approach, but it seems like that was too late in the process.

Instead, I just hooked into the normal form submit event and synced the hidden fields there. Then, when turbo:submit-start is fired, the form data is already there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to modify formData on turbo:submit-start #303 - bytemeta
I want to modify any form of my page when they are submitted. I tried to add some data to the form modifying...
Read more >
generic approach to add controllers from FormType classes ...
My root form has controller unload that will ask user to confirm turbo:visit if FormData has been changed. And it works but the...
Read more >
Form validation in Rails - DEV Community ‍ ‍
We'll first set up our User model, then UsersController , then we'll move ... async validate(event) { const formData = await event.detail.
Read more >
RubyGems - turbo-rails - Versions diffs - 1.0.0 → 1.0.1 - Diffend
Load your application's JavaScript bundle inside the <head> element instead. <script> elements in <body> are evaluated with each page change. @@ -21,5 + ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found