komlanvi

komlanvi

LiveView phx-change attribute does not emit event on input text

Hi everyone,

I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the text change. So I added phx-change attribute on every input (I only have text, email and password field) but it does not emit any event when the text of any of my input field change. Strangely It emits an event when instead of phx-change I use phx-click. Am I adding the wrong attribute?
On React I used to add onChange={myCallback} attribute on text input, is there something similar? I checked phoenix_live_view.js doc but can’t find a solution. Could someone help me?

Marked As Solved

chrismccord

chrismccord

Creator of Phoenix

At the moment I’m not planning on making it per input because you can get what you want with the form events, and every event will have all the form input so it’s generally a better approach.

Also Liked

OvermindDL1

OvermindDL1

My initial thought is an autocomplete input, I want the input code to handle that, not the form itself as it doesn’t care, and adding that to every form’s handler just in the off chance the autocomplete version is used in the HTML is extra mental bandwidth to handle, plus however many other plugins the overall system will include over time. The autocomplete input is the precise use that I use a drab submodule for. In HTML all I have to do is craft an html element with special drab-* attributes and it auto-registers and hooks in the child commander, which uses the information in the attributes to know which autocomplete list to pull from (which can even push updates in real time without typing changes if the autocomplete list has backend changes). :slight_smile:

10
Post #6
Maxim-Filimonov

Maxim-Filimonov

Chiming in here with several use cases we have run into while developing our app.
We use Surface heavily which pushed us a lot into using live_component. Which worked great… Until we realized that we cannot handle any input changes inside our live_component without adding a form. And as soon as we try to put live_component inside another form it all breaks as forms cannot be nested based on HTML5 specification.

We have many autocomplete and select fields and need to resort to writing lots of custom hooks with push events through alpine just to work around it.
I do feel that it shows that the current implementation of live_component needs more work. Specifically, the change event based on HTML5 specification is raised on the element, not on the form. It only bubbles to the form:

When the user agent changes the element’s value on behalf of the user (e.g. as part of a form prefilling feature), the user agent must follow these steps:

  1. If the input event applies, queue a task to fire a simple event that bubbles named input at the input element.
  2. If the change event applies, queue a task to fire a simple event that bubbles named change at the input element.

A simple use case that is trivial in React or even plain javascript seems quite problematic to make work with live_component.
We have a live_component with pairs of select input and text input. When a certain select value is selected the input field should be hidden. Right now we hack it with this:

<Select
              class="w=1/3 border bg-white rounded py-2 outline-none"
              options={space_type_options(index)}
              opts={
                "x-model": "selectedType",
                id: "subpremiseSelector2",
                "phx-hook": "Inputs.SubpremiseInputs#subpremiseInputs",
                "x-on:change": "subpremiseInputs.pushEventTo($el, 'type_changed', $el.value)"
              }
            />

    <div data-show={@show} x-show="selectedType!=$el.dataset.show">
            <Field name={:value}>
              <TextInput class="border bg-white rounded py-2 outline-none" opts={hook: "subpremiseInputs"} />
              <ErrorTag />
            </Field>
          </div>

# the hook
let subpremiseInputs = {
  mounted() {
    window.subpremiseInputs = this;
  },
};

window.subpremiseInputs = subpremiseInputs;
export { subpremiseInputs };

Being able to just have phx-change on select and handle_event inside the live_component would remove the need for custom hook hacking and adding alpine magic where its not necessary needed.

dvic

dvic

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement