cantiero

cantiero

I am creating a table, where users may edit some fields just by typing and changing their text. It is a functional component that my be placed in a Live Component or in a Live View. My idea is to not use forms for it, just having the input tag send the changes to the server using phx-change and dealing with it from there.

The component that renders a cell looks like this:

<td>
  <input
    type="text"
    id={"#{@field_name}--#{@row_id}"}
    name={"table[#{@field_name}--#{@row_id}]"}
    value={@value}
    phx-debounce={500}
    phx-change={assigns[:"phx-change"]}
    {Map.take(assigns, [:"phx-target"])}
  />
</td>

But, when I try to use this in a LiveView without passing a phx-target, as I want the event to be sent to the live view, the error Uncaught TypeError: Cannot read properties of null (reading 'getAttribute')... is raised by view.js at the line let cidOrSelector = target.getAttribute(this.binding("target")) in the targetComponentID method.

Not sure if this has some relation to this input not having form ancestor, or if I am missing something. Any help would be greatly appreciated.

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

Any reason for that? You can easily assign inputs to a form elsewhere in the page with the form attribute, so no need to wrap the whole table in a form.

cantiero

cantiero OP

I just try to keep my markup and components as clean as possible. The table may be used in a view with no editable fields and I did not want to add a form element as its parent when it is not needed. The solution to accomplish that was looking ugly IMO.

But the form attribute solves that in an acceptable way for me. I simply did not remember it even existed.

I would prefer not having to include this form element as it makes the functionality depend on two separate components sharing the id information to make an event in one of them work. The form tag, in theory, should not need to exist. But that is just me being extremely picky.

Thank you for you help!

LostKobrakai

LostKobrakai

Why would the table be aware of it’s contents in the first place? I’d imagine it like this:

<.form id="a" …></.form>
<.table>
  <:col>
    <.input form="a" …>
  </:col>
</.table>

No need to share any implementation details across components.

cantiero

cantiero OP

My td subcomponent was made only to be used by the table component. For now I only need one table per view. So, it all looks like this:

table component is used as:

<.table rows={@rows} fields={@fields} />

table.html.heex renders:

<form id="table--form" />
<table class="table">
  <thead class="table--header">
    <tr class="table--header_row">
      <th :for={field <- @fields} class="table--header_cell">
        <%= field.label %>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr :for={row <- @rows} class="table--row">
      <.td
        :for={field <- @fields}
        value={row[field.name]}
        type={field.type}
        field_name={field.name}
        row_id={row.id}
        {Map.take(field, [:"phx-change", :editor])}
        {Map.take(assigns, [:"phx-target"])}
      />
    </tr>
  </tbody>
</table>

When attribute editor = :textbox, td.ex renders:

<td>
  <input
    type="text"
    form="table--form"
    id={"#{@field_name}--#{@row_id}"}
    name={"table[#{@field_name}--#{@row_id}]"}
    class="td--text_input"
    value={@value}
    phx-debounce={500}
    phx-change={assigns[:"phx-change"]}
    {Map.take(assigns, [:"phx-target"])}
  />
</td>

I agree with you: I could instead add a form attribute to td. But the ideal world for me would be not having to add the form tag in table, nor the form attribute in td, nor the form attribute in the input. Reducing boilerplate, having a cleaner code.

nicklayb

nicklayb

For anyone coming across this thread with a usecase where you wanna use a form input without form, it’s achievable with a hook pretty simply, here’s what I came up with

The Javascript hook (named Formless)

export default {
  mounted() {
    this.el.addEventListener('change', event => {
      const eventName = this.el.dataset.event

      this.pushEvent(eventName, { value: event.target.value })
    })
  }
}

The usage

<select data-event="selectUpdated" phx-hook="Formless" id="some-id">
 ...
</select>

And when the select gets updated it fire the selectUpdated with %{"value" => the_value}.

briancbarrow

briancbarrow

Thank you for this! I thought I was losing my mind. Good to know it isn’t supported.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
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
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

Potions
A new screencast on live LiveView updates with PubSub, the Oban Web dashboard, and smarter retries. Part 2 of 2. Screencast + full walkth...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews