View Source PolymorphicEmbed.HTML.Form (Polymorphic Embed v5.0.0)

Summary

Functions

Link to this function

get_polymorphic_type(form_field)

View Source

See PolymorphicEmbed.HTML.Helpers.get_polymorphic_type/1.

Link to this function

get_polymorphic_type(form, field)

View Source

See PolymorphicEmbed.HTML.Helpers.get_polymorphic_type/2.

Link to this function

polymorphic_embed_inputs_for(form, field)

View Source

Generates a new form builder without an anonymous function.

Similarly to Phoenix.HTML.Form.inputs_for/3, this function exists for integration with Phoenix.LiveView.

Unlike polymorphic_embed_inputs_for/4, this function does not generate hidden inputs.

Example

<.form
  :let={f}
  for={@changeset}
  id="reminder-form"
  phx-change="validate"
  phx-submit="save"
>
  <%= for channel_form <- polymorphic_embed_inputs_for f, :channel do %>
    <%= hidden_inputs_for(channel_form) %>

    <%= case get_polymorphic_type(reminder_form, Reminder, :channel) do %>
      <% :sms -> %>
        <%= label channel_form, :number %>
        <%= text_input channel_form, :number %>

      <% :email -> %>
        <%= label channel_form, :email %>
        <%= text_input channel_form, :email %>
  <% end %>
</.form>
Link to this function

polymorphic_embed_inputs_for(form, field, fun)

View Source

Like polymorphic_embed_inputs_for/4, but determines the type from the form data.

Example

<%= inputs_for f, :reminders, fn reminder_form -> %>
  <%= polymorphic_embed_inputs_for reminder_form, :channel, fn channel_form -> %>
    <%= case get_polymorphic_type(reminder_form, Reminder, :channel) do %>
      <% :sms -> %>
        <%= label poly_form, :number %>
        <%= text_input poly_form, :number %>

      <% :email -> %>
        <%= label poly_form, :email %>
        <%= text_input poly_form, :email %>
    <% end %>
  <% end %>
<% end %>

While polymorphic_embed_inputs_for/4 renders empty fields if the data is nil, this function does not. Instead, you can initialize your changeset to render an empty fieldset:

changeset = reminder_changeset(
  %Reminder{},
  %{"channel" => %{"__type__" => "sms"}}
)
Link to this function

polymorphic_embed_inputs_for(form, field, type \\ nil, fun)

View Source

See PolymorphicEmbed.HTML.Helpers.source_data/1.

See PolymorphicEmbed.HTML.Helpers.source_module/1.

Link to this function

to_form(source_changeset, form, field, options)

View Source

See PolymorphicEmbed.HTML.Helpers.to_form/4.