-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
base: main
Are you sure you want to change the base?
Conversation
Been sitting on this one for a while, but will turn my attention back to it soon. Thanks for being patient. |
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. |
@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. |
Awesome, @hopsoft ! I can't wait to see what you're cooking up! |
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. |
Looking forward to it! |
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)
}
} |
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. |
Thanks for the update, @hopsoft! Where can I see an example of your implementation? |
By adding
data-turbo-command-serialize-form="true"
to any HTML tag with adata-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"
ordata-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
orcontroller.params
, just like one would normally do if a form was submitted.