PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

Feature Request: Ignore "change" events of specific form inputs in LiveView

Hey folks, I have the unique problem that I need to ignore all “change” and “input” events for one specific input element in a LiveView Form because these events are handled by a 3rd party JS library. That library (QuillJS) adds its own eventListener to the input field which creates a race condition with LiveView’s eventListener.

An example:

<.form for={@form} phx-change="validate">
  # Changes to this input should send the "validate" event.
  <.input form={@form[:name]} type="text" />

  # Changes to this input should be ignored and not send the "validate" event.
  <select contenteditable="false">
    <option value="foo">Foo</option>
    <option value="bar">Bar</option>
  </select>
</.form>

I tried these solutions, but none of them worked:

  1. Put phx-update="ignore" on the select input.
    • The phx-update flag only ignores changes coming from the server, not from the client to the server.
  2. Put a custom phx-change="ignore" attribute on the select input.
    • This still sends an “ignore” event to the server which sends back an update which breaks my 3rd party library (quill editor calling highlight.js for syntax highlighting)
  3. Put onchange and oninput attributes with an event.stopPropagation() or event.preventDefaults() call on the select input.
    • These don’t seem to have an effect. I guess because LiveView puts an form.on("change", fn) listener on the form.
  4. Add a custom eventListener to the select-input which calls event.stopPropagation() or event.preventDefaults().
    • Had no effect. The “validate” event was still sent to the server.
  5. Remove the `phx-change=“validate” from the form altogether.
    • That worked, but now I can’t validate the form before it’s submitted.

Ideally, LiveView would respect an phx-update="ignore" attribute and ignore any “change” and “input” events from that input, which means not sending a “validate” event back to the server.

The code in question

https://github.com/phoenixframework/phoenix_live_view/blob/48408aa288b70025994f28ad43912c6d76f40aa3/assets/js/phoenix_live_view/live_socket.js#L856

Prior discussions about this topic

https://github.com/phoenixframework/phoenix_live_view/issues/3153

Most Liked

LostKobrakai

LostKobrakai

Sounds like LV should ignore input events from inputs without names. Their values won’t make it to the server anyways, because inputs without names don’t send data within a form.

vtno

vtno

@chrismccord It works!

@PJUllrich What I did was adding this hook and use it on the input I don’t want to trigger phx-change

const StopPropagation = {
  mounted() {
    events = this.el.dataset.stopEvent.split(",");

    events.forEach(event => {
      this.el.addEventListener(event, (event) => {
        console.debug('StopPropagation stopped', event);
        event.stopImmediatePropagation();
        event.stopPropagation();
      });
    });
  }
};

export default StopPropagation;

# markup code
<input
    id={"email_checkbox_#{idx}"}
    type="checkbox"

    # other attrs...

    data-stop-event="change,input"
    phx-hook="StopPropagation"
/>

This is quite useful when building a complex form where some sections are toggled with checkbox or etc.

LostKobrakai

LostKobrakai

An input without a name would already not be sent, because there’s no key. You cannot encode what doesn’t exist.

Where Next?

Popular in Proposals: Ideas Top

Gladear
Hello everyone! tldr; I propose to add a way for VerifiedRoutes to know if they’re used for get, post, etc to make them even more robust...
New
cevado
I was reading the EEP-79, and thinking about the poor record support in Elixir(i’ve tried to discuss about that in the forum before). I s...
New
AHBruns
Rethinking phx-no-feedback So, I won’t go into how phx-no-feedback works in detail, but the tl;dr is that it lets you conditionally add a...
New
andreamancuso
Hey folks, This might sound niche, but I think it’s worth bringing up - especially given Phoenix’s reputation for being lightweight, por...
New
andypearson
Hey all, I have been working on some performance improvements for the Phoenix application I work on. As part of this, through trial and...
New
hst337
iex&gt; map = %{x: 1} iex&gt; map.x 1 iex&gt; map.x() 1 Why would anyone use the map.x() syntax for getting map value? I’d suggest depre...
New
cgraham
Please change passwordless login from a link to a 6-8 digit code a user can enter. Gen Auth in the latest RC 1.8 of Phoenix defaults ...
New
superchris
Currently there is no out of the box way to support DOM Custom Events in phoenix. I’ve created a separate library to do this, but I’d lov...
New
New
sevensidedmarble
I have a very simple suggestion: the generated config/dev.exs file should read from PORT at compile time to set the endpoints port. If ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement