Skip to content

How is one supposed to display the polymorphic embed fields in a form if the schema is empty?聽#73

Description

@maxmarcon

First of all, thanks a ton for this great library 馃憤

I have the following schema (abridged):

defmodule ClientApiConfig do 

 schema "client_api_config" do

 polymorphic_embeds_one(:args,
      types: [
        legal_tracker: DataIngestion.LegalTracker.ApiArgs,
        counsellink: DataIngestion.Counsellink.ApiArgs,
        ebiller_mock: DataIngestion.Api.DummyArgs
      ],
      on_replace: :update,
      on_type_not_found: :changeset_error
    )
 end
end

Then in my HTML form, following the LiveView example I have:

<%= for args_form <- polymorphic_embed_inputs_for(f, :args) do %>
        <%= hidden_inputs_for(args_form) %>
        <%= case get_polymorphic_type(f, ClientApiConfig, :args) do %>

       <% :counsellink -> %>
           ... inputs for counsellink

      <% :legal_tracker -> %>
           ...inputs for legal_tracker 

      <% end %>
<% end %>

(sidenote, I think the LiveView example is wrong, it should be get_polymorphic_type(f, Reminder, :channel) and not get_polymorphic_type(channel_form, Reminder, :channel))

The above form works if I'm editing new data which already contains an :args embed. If however I want to edit a new record and I start from an empty %ClientApiConfig{}, no fields will be rendered because polymorphic_embed_inputs_for(f, :args) returns [] (there's no :args embed and therefore no :__type__ field in it, I guess).

How can I enforce a default type? I can do that with polymorphic_embed_inputs_for/4, but since I'm using LV, I thought I'm supposed to use polymorphic_embed_inputs_for/2. Or perhaps I should use the undocumented to_form/5?

Seems to me that polymorphic_embed_inputs_for/2 should be extended to accept a type parameter.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions