Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally serialize form or partial form when invoking command #83

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cmer
Copy link

@cmer cmer commented Jan 30, 2023

By adding data-turbo-command-serialize-form="true" to any HTML tag with a data-turbo-command, the closest form values will be serialized and included in the payload sent to the server.

Alternatively, one can specify a CSS selector to send a subset of a form in the payload. For example: data-turbo-command-serialize-form=".foo" or data-turbo-command-serialize-form="#foo" will only serialize the form input elements contained within the selector.

On the server side, the form values can be accessed via params or controller.params, just like one would normally do if a form was submitted.

@hopsoft
Copy link
Owner

hopsoft commented Jun 23, 2023

Been sitting on this one for a while, but will turn my attention back to it soon. Thanks for being patient.

@cmer
Copy link
Author

cmer commented Jun 23, 2023

Sounds good! I know you initially had reservations about this pattern, but I still think it's one worth having. It makes building truly reactive forms so much easier.

@hopsoft
Copy link
Owner

hopsoft commented Oct 3, 2023

@cmer You'll be excited to know that I'm putting some attention on this; though, my solution for it is a bit different than what you have here.

@cmer
Copy link
Author

cmer commented Oct 4, 2023

Awesome, @hopsoft ! I can't wait to see what you're cooking up!

@hopsoft
Copy link
Owner

hopsoft commented Dec 21, 2023

I haven't forgot about this request and PR. I've just been pulled away from much TurboBoost work of late given some of my day job responsibilities. I hope to revisit this in the new year though.

@cmer
Copy link
Author

cmer commented Dec 27, 2023

Looking forward to it!

@hopsoft
Copy link
Owner

hopsoft commented Feb 3, 2024

I haven't stood up my PR for this yet, but I have run into a requirement for attaching the form.

FWIW: Here's an Example stimulus controller that I currently attach to the element that invokes the command.

Note

I'm planning to bake something like this into the lib with an opt-in attribute.

import ApplicationController from './application_controller'

export default class extends ApplicationController {
  attachFormData(event) {
    this.element.dataset.form = this.formJSON
  }

  get form() {
    return this.element.closest('form')
  }

  get formData() {
    if (!this.form) return {}
    return Object.fromEntries(new FormData(this.form))
  }

  get formJSON() {
    return JSON.stringify(this.formData)
  }
}

@hopsoft
Copy link
Owner

hopsoft commented Oct 17, 2024

Update: The new monorepo has a very elegant solution for this. You can serialize form elements from anywhere on the DOM from multiple parents. They don't even need to be contained within a form. Will have something you can play around with soon.

@cmer
Copy link
Author

cmer commented Oct 17, 2024

Thanks for the update, @hopsoft! Where can I see an example of your implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants