View Source PolymorphicEmbed.HTML.Form (Polymorphic Embed v3.0.6)

Defines functions for using PolymorphicEmbed with Phoenix.HTML.Form.

Summary

Functions

Returns the polymorphic type of the given field in the given form data.

Generates a new form builder without an anonymous function.

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

Functions

Link to this function

get_polymorphic_type(form, schema, field)

View Source

Returns the polymorphic type of the given field in the given form data.

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(channel_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(channel_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, fun)

View Source
Link to this function

to_form(source_changeset, form, field, type, options)

View Source