3

Im rendering a nested (child) LiveView inside my main LiveView. Events inside the child LiveView gets sent to the parent instead of the child.

How do i achieve that the child LiveView receives the event and not the parent?

I am using phoenix_live_view 0.17.5.

Here is my setup:

The render inside my main LiveView:

def render(assigns) do
    ~H"""
      <%= live_render(@socket, MyProjectsWeb.HomeLive.CreateProjectForm, id: "create-project-form-liveview") %>
    """
  end

The render inside my child LiveView:

def render(assigns) do
    ~H"""
      <.form let={f} for={@changeset} phx-submit="create">
            <.field_label bind={f} for={:name} value={"Name"}/>
            <.text_field bind={f} name={:name} />
            <.primary_button type="submit">
              Create
            </.primary_button>
      </.form>
    """
  end

When i submit the Form in my child LiveView i get a error saying that the handle Event function in my parent LiveView is undefined.

I tried to add phx-target="create-project-form-liveview" to my form but when i do that the submit button doesnt do anything anymore and the Browser console says "nothing found matching the phx-target selector "create-project-form-liveview" undefined.

When i add phx-target="#create-project-form-liveview" to my form instead the event fires but still gets received by my main (parent) LiveView.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • not sure, because I never did this, but this may help: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-targeting-component-events – Dill Jan 15 '22 at 10:21
  • To my understanding this only applies to live_components but not to live_views. However i tried rewriting my live_view to a live_component and using the @myself target but the event still gets sent to the parent view. – Felix Wieland Jan 15 '22 at 13:22
  • Can you share more about the modules (code) and how we you are rendered them? – thelastinuit Oct 11 '22 at 15:26

0 Answers0