overcomeoj

overcomeoj

I have my forms setup as per the liveview form generators.

Sometimes I see this error occurring, where the form is posting with the hidden CSRF as the change target, which doesn’t have a handler, then the view collapses. It somewhat unnerving.

Is this a dev-hot-reloading issue? Should I have a dummy handle_event that does nothing and matches anything “just in case”?

no function clause matching in

Web.Live.Component.handle_event("validate", %{"_csrf_token" => "...", "_target" => ["_csrf_token"], "form" => %{"values" => "..."}},

Showing Posts 1 to 5

petelacey

petelacey

I have this exact issue. It’s rare enough that I haven’t noticed a pattern, but frequent enough to be noticeable. Are there any thoughts on what might be the culprit?

mcrumm

mcrumm

Phoenix Core Team

Most likely this is occurring due to the behaviour of form recovery following crashes or disconnects: the auto-recovered form has no context for the change event target, so it grabs the first form element and uses it as the target.

This could be verified by binding a custom handler for form recovery (by default it uses the phx-change binding):

<form phx-change="validate" phx-auto-recover="recover">

or with form helpers:

<%= f = form_for @changeset, "#", phx_change: "validate", phx_auto_recover: "recover" %>

then add the appropriate callback(s) to your LiveView:

def handle_event("recover", %{"_target" => ["_crsf_token"]} = params, socket) do
  # recover the form state after disconnect
  {:noreply, socket}
end
petelacey

petelacey

I can’t say I grok this, but I’ll give it a shot. I do have the liveview socket exposed on the window object as ls, and using the console to ls.disconnect() and ls.connect() did not reproduce the error. Which is good, I suppose, because otherwise auto-recover would never work.

Would it be too much to ask for some additional context around “the auto-recovered form has no context for the change event target” and why LV would grab the first form field (CSRF token) and use that as a target? Thanks!

mcrumm

mcrumm

Phoenix Core Team

Form recovery occurs when the socket rejoins following a disconnect or after recovering from an error. The form recovery event mimics a form input/change event however form events have a target property that indicates which form control dispatched the event. Recovery events have no such target, so we quite literally target the first form element.

The Phoenix.HTML.form_for helper by default generates a hidden input called "_csrf_token" directly following the <form> tag. This will always be the first form element unless you disable it with csrf_token: false.

Were you testing this in the dev environment? If so, did you disable LiveReload? There is a note about this in the link I shared above. FWIW I was definitely able to reproduce this purely client-side :slight_smile:

If it helps, I made a gist with a more complete example: LiveView Form Auto Recovery · GitHub

petelacey

petelacey

I very much appreciate you taking the time to reply, @mcrumm. It seems I wasn’t fully understanding your reply, because I actually don’t have the same issue as OP, just one that looks a lot like it.

However, this thread made me look more closely at the log output, and this (annoyingly rare and inconsistent) issue seems to be due to the phx-change event being sent to the wrong LiveView. This is likely my fault, but now I need to dig deeper. Anyway, thanks again.

— 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
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
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
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews