budgie

budgie

I have a custom LiveComponent form input that is built with 3 text inputs: one hidden and 2 visible. The hidden text input holds a duration in minutes for submission of the form. The other inputs are there for the user to interact with in order to modify the hour and minute value. I need to respond to change events of the 2 visible inputs in order to modify the hidden input value in minutes.

What’s the best way to respond to change events on “fake” form inputs like this? I just learned that phx-change events are not implemented for inputs. Thing is, this is a reusable input and I need to be able to break it apart from the normal form. Should I just do this with javascript, or is there a better way to keep it in LiveView?

Showing Posts 1 to 9

LostKobrakai

LostKobrakai

Imo the cleanest, but not necessarily most trivial, solution would be a webcomponent, which acts like a native input – e.g. emits change/input events for phoenix to pick up. This would also be a place to handle the two separate inputs fields modeled with a single value.

sodapopcan

sodapopcan

No but key events are!

LostKobrakai

LostKobrakai

You may wish for an individual input to use its own change event or to target a different component. This can be accomplished by annotating the input itself with phx-change, …

budgie

budgie OP

Have tried using it on the input itself as outlined but I’m getting some strange params with the event.
%{"_target" => ["undefined"]}. Any idea why the value is not coming through?

 <input
  type="number"
  min="0"
  max="24"
  class="w-[1ch] text-shady-white caret-bright-purple border-none bg-transparent focus:ring-0 p-0"
  phx-change="modify_duration"
  phx-target={@myself} />
sodapopcan

sodapopcan

Well I’ll be. I haven’t RTFM’d those parts in a while.

LostKobrakai

LostKobrakai

You got no name on the input.

superchris

superchris

This sounds like an ideal use case for a form associated custom element. This is a web component that takes advantage of some apis (well supported in all the browsers by now) that allow it to behave like an actual form input, binding it’s values to the form and such. Since your element is an actual form input all of the normal phx form events should work properly.

I’ve been working on something similar you might be able to use as an example: a more self-contained autocomplete element that binds its value directly to the form rather than requiring hidden input shenanigans. It’s a working but not as well documented yet as I would like, but feel free to hit me up with questions.

Here’s the element itself: GitHub - launchscout/autocomplete-input: An event oriented, autocomplete form associated custom element · GitHub

I’ll work on getting an example of using it in LiveView in a sharable state if there is interest.

garrison

garrison

You could also just write a hook that sends events to the server, which is sufficient for a lot of simple controls like this. You could even update the hidden input’s value in JS and send nothing at all.

However - and maybe I’m misunderstanding something here - in this case could you not just send the hours/minutes to the server and then perform the (h * 60) + m upon form submission? The value is hidden, so it’s not like the user needs to be able to see it. Why include it at all?

budgie

budgie OP

For anyone coming to this later, I opted for a custom element. Right now it’s in vanilla JS but I don’t like all the boilerplate so I’m going to turn it into a Lit component at some point.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews