Closed PJUllrich closed 6 days ago
My understanding is that the dispatch should be emitted from the input element and not the form itself.
The following adjustment works:
<button type="button" phx-click={JS.dispatch("input", to: "##{form[:number].id}")}>Dispatch</button>
But maybe it should also work at the form level 🤷
True, that works for me too! Interesting 🤔 I'll keep this issue open for now, but that might be the answer indeed 👍
@PJUllrich dispatching the event to the input element is the way to go. LiveView only expects input events on actual inputs (i.e. input, select, textarea). I also created a PR for a more useful error message. With it, the error would have been:
Error: dispatching a custom input event is only supported on input elements inside a form
which is hopefully more helpful :)
Environment
Actual behavior
I try to force an
phx-change
event using a button from within a Phoenix.HTML.Form, but when I call aJS.dispatch("input", to: "#some-form")
from inside a<.form id="some-form" ... >
tag, the console shows a JS exception:You can reproduce this using this little script:
Save the script to e.g.
bug.exs
and execute it withelixir bug.exs
. You'll see that the"change"
event works well if you change the number input, but if you click on the button, you'll see the JS exception in the browser console.This is likely related to https://github.com/phoenixframework/phoenix_live_view/issues/2916 but in this case, the input is actually wrapped by a proper
<.form>
tag and uses aPhoenix.HTML.Form
under the hood. I also replicated this bug with anEcto.Changeset
as a source for thePhoenix.HTML.Form
. Same bug happened.Expected behavior
Clicking the button should cause the form to emit a
validate
event and send the currently filled-in parameters to the LiveView, just like any other input would.