cantiero
Formless input phx-change in live view
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.
Marked As Solved
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.
Also Liked
mxgrn
Formless inputs are not allowed in LiveView: Prevent exception with inputs without a form by mveytsman · Pull Request #2343 · phoenixframework/phoenix_live_view · GitHub
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}.
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.
Last Post!
briancbarrow
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










