LiveView Issue with mix.phx.gen live files

I’ve been building an app with Phoenix 1.7 and LiveView and ran into some behavior that has me a little perplexed. I generated a live view with mix phx.gen.live and made a small change to the form_component.ex file. I simply wanted to add additional handle_event functions to add functionality to the form. In particular, I added a search event handler. What was perplexing to me was that I kept getting errors that no function pattern matched the function I was trying to call in the form. After a bit I realized that the app was looking for an event handler not in FormComponent but in Index. When I copy paste the exact event handler to Index everything works. So my question is, why is that? There are event handlers in the default form_component file, so why if I add another event handler below those functions does it not get registered and looks to Index instead? Any help is appreciated.

The behavior you’ve described sounds like what happens when a live component adds an event handler to the markup (via phx-click="something") but doesn’t specify phx-target.

The events already hooked up in the generated form_component include a target:

@myself is an assign that’s automatically set when a live component is rendered.

That’s what it was! Thank you!

You were facing more or less the same issue I’ve been facing. If the simple_form already has a phx-target={@myself}, what should the phx-target be for a button that, when clicked, adds more fields to the form?