How to get `phx-click` to get handled by `FormComponent` instead of `Index`

I have generated the default live view using

mix phx.gen.live

I added a button in the form_component.html.heex with a phx-click attribute,

When I define handle_event/3 inside the FormComponent module I get the error no function clause matching but when I define handle_event/3 inside the Index module it works …


I want to handle_event inside the FormComponent module because it contains the changeset from the user input, and I can not access the changeset from the Index module.

the router.ex file also contains the default value:

live "/items/new", ItemLive.Index, :new

You just need to add a phx-target to that button to send the click event to the right target. For that you will use the id of your form component.

phx-target=β€œ#my-component-form”

1 Like