tehwilsonator

tehwilsonator

LiveView propagate phx-change on hidden_input

Hello! I have a LiveView generated on “scheduler” model with --live, where the model has (among others) a “date_range_start” field that is a Date type. The form_component generated with date_select inputs, and I have been working on changing those into a calendar-picker LiveComponent. It’s been going great, except I’m having trouble getting the phx-change event (“validate”) to propagate when the hidden_inputs that I’ve slotted into the Calendar component change, and I’m trying to figure out why since those hidden_inputs are clearly tied to the form etc.

Some code:


  <.form
    let={f}
    for={@changeset}
    id="scheduler-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save">
  
    <%= label f, :title %>
    <%= text_input f, :title %>
    <%= error_tag f, :title %>
  
    <%= label f, :date_range_start %>
    <.live_component
      module={MyApp.SchedulerLive.CalendarComponent}
      id="scheduler-calendar-date_range_start"
      form={f}
      let={current_date}
    >
      <%= hidden_input f, :date_range_start, name: "#{f.name}[date_range_start][year]", value: Timex.format!(current_date, "%Y", :strftime) %>
      <%= hidden_input f, :date_range_start, name: "#{f.name}[date_range_start][month]", value: Timex.format!(current_date, "%-m", :strftime) %>
      <%= hidden_input f, :date_range_start, name: "#{f.name}[date_range_start][day]", value: Timex.format!(current_date, "%-d", :strftime) %>
    </.live_component>
    <%= error_tag f, :date_range_start %>
  
    <%= label f, :date_range_end %>

    <%= date_select f, :date_range_end %>
    <%= error_tag f, :date_range_end %>
  
    <div>
      <%= submit "Save", phx_disable_with: "Saving..." %>
    </div>
  </.form>

The CalendarComponent has no trouble choosing a current_date, and I can see the generated input type="hidden" tags changing when it does, but no "validate" event is getting handled by the LiveComponent when it does so (it is handled for all the other inputs, including that second date_select that I haven’t replaced yet). I turn to the broader community, humbly, for help with this.

Most Liked

tehwilsonator

tehwilsonator

The documentation pointed to above gives the building blocks but doesn’t put them together, so for posterity, I added a Hook with this:

Hooks.PublishInput = {
  updated() {
    this.el.dispatchEvent(
      new Event("input", {bubbles: true})
    )
  }
}

then my hidden_input tags within the slot for the calendar component got this:

      <%= hidden_input f,
        :date_range_start,
        id: "scheduler-date-range-start-year",
        'phx-hook': "PublishInput",
        name: "#{f.name}[date_range_start][year]",
        value: Timex.format!(current_date, "%Y", :strftime)
      %>

From here it can be abstracted into a helper to tidy it up, but this got me to functional. Thanks again, LostKobrakai!

LostKobrakai

LostKobrakai

Setting form values from JS by default doesn‘t trigger change events (for legitimate reasons/usecases), so you need to manually trigger them for phoenix to pick up: JavaScript interoperability — Phoenix LiveView v1.2.5

Last Post!

Manukam

Manukam

I tried exactly this, still does not seem to trigger the validate function. Is there a way to make sure that the event is fired?

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
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
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
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
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement