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

Add before callback to form link #507

Conversation

condef5
Copy link

@condef5 condef5 commented Dec 24, 2021

This pr adds a callback to the form link that is executed in convertLinkWithMethodClickToFormSubmission.

With this callback we could add some custom behavior like this:

Turbo.setLinkToFormBeforeCallback((form) => {
  if (form.getAttribute('method') !== 'delete') return;
  
  const input = document.createElement("input");
  input.setAttribute("name", "_method")
  input.setAttribute("type", "hidden")
  input.setAttribute("value", "delete")
  
  form.method = 'post'
  form.appendChild(input)
})

This little script converts the links with the delete method to post and also adds a _method field with a delete value in the form link.

Notes:

  1. We also could add this script to turbo-rails in order to fix this "data-turbo-method: delete" should use POST with _method rather than DELETE turbo-rails#259
  2. I'm open to suggestions to find a better name 😅

@dhh
Copy link
Member

dhh commented Jul 18, 2022

Given that this really is just to be used for the specific case of using _method field with delete for Rails, I think we should just bake that implementation in, exposed a flag to flip that setting, ala Turbo.encodeHiddenLinkFormMethod = true, which turbo-rails can then trigger. Other frameworks may indeed want to follow suit on this.

@dhh dhh added the needs work label Jul 18, 2022
@seanpdoyle
Copy link
Contributor

Could turbo-rails support this directly, with an event listener like the one introduced in https://github.com/hotwired/turbo-rails/pull/239/files#diff-f438134433fd9897751d1b5cd219ea33ac29be8f3af3e4ac17e65475e4cadbeeR1-R7?

@brunoprietog
Copy link
Collaborator

Hi @condef5, Thanks for the proposal, but I'm closing this for now. It's possible to do something similar with event listeners as well.

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

Successfully merging this pull request may close these issues.

4 participants