Sanjibukai

Sanjibukai

Phx-change on input themselves

Hello everybody,

I have a form with many inputs..
One of those inputs (for example a date or a quantity) affects for example a total price. I wanted to display that estimated price only when the inputs related to the price are changing.

Using phx-change at the form level will trigger so many events because of all the other inputs (I don’t even think for textarea being typed) and I don’t want that. And using debounce will make me lose the reactivity I want for the only fields related to the total.

Using phx-blur on those input works, but the user needs to leave those input fields which may not occurs immediately particularly when using for example the built-ins arrows for numeric input fields, or when manipulating the built-ins calendars.

Am I the only one who don’t understand why phx-change doesn’t apply for inputs themselves only?

If I should bring some JS in order to do that, I lose the main purpose of why I want to use Live View…

Anyway, is there some kind of workaround I can do?

I thought of making many little forms (at least for the fields participating to the total), but I’m using a changeset.
Currently, when I’m using phx-blur, I’m updating the changeset in which there is both the total and the related quantity or date fields. So upon the render, I have nothing to do.
So I want to keep the whole changeset based form.

I even tried to nest the inputs field within a bare <form phx-change> ... </form>..
But indeed it’s not correct HTML, and the form doesn’t even display in the source..

Marked As Solved

sfusato

sfusato

The debounce is set per input. For the inputs that you want to ignore for the phx-change action, set it to blur and have the handle_event callback pattern match those (or pattern match those you’re interested in instead and have a catch-all that ignores everything else) and simply return the unmodified socket in that case.

Also Liked

sfusato

sfusato

You can pattern match based on the input that triggered the phx-change action.

Example from the docs:

For example, if the following input triggered a change event:

<input name="user[username]"/>

The server’s handle_event/3 would receive a payload:

%{"_target" => ["user", "username"], "user" => %{"username" => "Name"}}

Then you can have the following callbacks defined:

def handle_event("phx_change_action", %{"_target" => ["user", "username"], "user" => %{"username" => username}}, socket) do
... handle logic when username is changed
end
def handle_event("phx_change_action", %{"_target" => ["user", "quantity"], "user" => %{"quantity" => quantity}}, socket) do
... handle logic when quantity is changed
end

…and so on…

You can achieve a per input change like this. The idea is that you are not forced into only one handle_event callback. You can achieve the level of granularity that you wish by simply pattern matching on the payload.

Last Post!

Sanjibukai

Sanjibukai

I forgot (or even didn’t really notice) the blur option.
Indeed, this way, I was able to get the behavior I want..

Thanks for the insight..

Now I can ditch all that spaghetti JS I wrote :sweat_smile:

Where Next?

Popular in Discussions Top

New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement