miguelcoba

miguelcoba

Polymorphic_embed for LiveView 1.1

Does anyone have a working example of polymorphic_embed working with LiveView 1.1? Specifically the live view part (the backend part seems to work just fine).

I have followed the documentation but I see nothing on the browser. Nothing is emitted on the html if I inspect it.

Looking at the github issues/pull requests, it seems like it is kinda abandoned for the last year.

Any pointers to a working example or a fork that works for current (1.1) live view?

Thanks!

First Post!

brownerd

brownerd

I’ve been using it, but in a project that’s still in development. Its working great for me. Do you have any code to post, like the Schema and LiveView that use polymorphic_embed?

I do remember being confused about the usage, but it working.

Most Liked

brownerd

brownerd

Cool! Sorry for the late response, but I’m glad you figure it out. Like I said, I also remember running into some walls getting it to work the way I though it was supposed to. Like you, I eventually got it working and it has been great for me ever since. Best regards!

miguelcoba

miguelcoba

Ok, I think I got it to work:

I added a default to the client_type:

    field :client_type, Ecto.Enum, values: [:legal_entity, :individual], default: :legal_entity

Then the new puts the correct embedded struct, depending on the `client_type`:



  defp apply_action(socket, :new, _params) do
    client = %Client{user_id: socket.assigns.current_scope.user.id}

    client =
      case client.client_type do
        :legal_entity ->
          %Client{client | client_name: %LegalEntityName{}}

        :individual ->
          %Client{client | client_name: %IndividualName{}}
      end

    socket
    |> assign(:page_title, gettext("New client"))
    |> assign(:client, client)
    |> assign(:form, to_form(Clients.change_client(socket.assigns.current_scope, client)))
  end



And when validating, if the `client_type` changed, I change the polymorphic_embed struct too:


  @impl true
  def handle_event("validate", %{"client" => client_params}, socket) do
    changeset =
      Clients.change_client(socket.assigns.current_scope, socket.assigns.client, client_params)

    changeset =
      case Ecto.Changeset.get_change(changeset, :client_type) do
        nil -> changeset
        :legal_entity -> Ecto.Changeset.put_change(changeset, :client_name, %LegalEntityName{})
        :individual -> Ecto.Changeset.put_change(changeset, :client_name, %IndividualName{})
      end

    {:noreply, assign(socket, form: to_form(changeset, action: :validate))}
  end

The problem with replacing the client_name on the changeset when `client_type` changes is that the previous values are lost, but is something I can live with.

Thanks!

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New