defmodule <%= inspect Module.concat(@web_module, @resource_alias) %>Live.Form do use <%= @web_module %>, :live_view @impl true def render(assigns) do ~H""" <.header> <%%= @page_title %> <:subtitle>Use this form to manage <%= @resource_singular %> records in your database. <.form for={@form} id="<%= @resource_singular %>-form" phx-change="validate" phx-submit="save" > <%= cond do %> <% @create_action && @update_action -> %> <%= if @create_inputs == @update_inputs do %> <%= @create_inputs %> <% else %> <%%= if @form.source.type == :create do %> <%= @create_inputs %> <%% end %> <%%= if @form.source.type == :update do %> <%= @update_inputs %> <%% end %> <% end %> <% @create_action -> %> <%= @create_inputs %> <% @update_action -> %> <%= @update_inputs %> <% end %> <.button phx-disable-with="Saving..." variant="primary">Save <%= @resource_human_singular %> <.button navigate={return_path(@return_to, @<%= @resource_singular %>)} >Cancel """ end @impl true def mount(params, _session, socket) do <%= @resource_singular %> = case params["id"] do nil -> nil id -> Ash.get!(<%= inspect Module.concat(Elixir, @resource) %>, id<%= @actor_opt %>) end action = if is_nil(<%= @resource_singular %>), do: "New", else: "Edit" page_title = action <> " " <> <%= inspect @resource_human_singular %> {:ok, socket |> assign(:return_to, return_to(params["return_to"])) |> assign(<%= @resource_singular %>: <%= @resource_singular %>) |> assign(:page_title, page_title) |> assign_form()} end defp return_to("show"), do: "show" defp return_to(_), do: "index" @impl true def handle_event("validate", %{"<%= @resource_singular %>" => <%= @resource_singular %>_params}, socket) do {:noreply, assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, <%= @resource_singular %>_params))} end def handle_event("save", %{"<%= @resource_singular %>" => <%= @resource_singular %>_params}, socket) do case AshPhoenix.Form.submit(socket.assigns.form, params: <%= @resource_singular %>_params) do {:ok, <%= @resource_singular %>} -> notify_parent({:saved, <%= @resource_singular %>}) socket = socket <%= cond do %> <% @update_action && @create_action -> %> |> put_flash(:info, "<%= @resource_human_singular %> #{socket.assigns.form.source.type}d successfully") <% @update_action -> %> |> put_flash(:info, "<%= @resource_human_singular %> updated successfully") <% @create_action -> %> |> put_flash(:info, "<%= @resource_human_singular %> created successfully") <% end %> |> push_navigate(to: return_path(socket.assigns.return_to, <%= @resource_singular %>)) {:noreply, socket} {:error, form} -> {:noreply, assign(socket, form: form)} end end defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) defp assign_form(%{assigns: %{<%= @resource_singular %>: <%= @resource_singular %>}} = socket) do form = <%= cond do %> <% @update_action && @create_action -> %> if <%= @resource_singular %> do AshPhoenix.Form.for_update(<%= @resource_singular %>, <%= inspect @update_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>) else AshPhoenix.Form.for_create(<%= @resource %>, <%= inspect @create_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>) end <% @update_action -> %> AshPhoenix.Form.for_update(<%= @resource_singular %>, <%= inspect @update_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>) <% @create_action -> %> AshPhoenix.Form.for_create(<%= @resource_singular %>, <%= inspect @create_action.name %>, as: <%= inspect @resource_singular %><%= @actor_opt %>) <% end %> assign(socket, form: to_form(form)) end defp return_path("index", _<%= @resource_singular %>), do: ~p"<%= @route_prefix %>" defp return_path("show", <%= @resource_singular %>), do: ~p"<%= @route_prefix %>/#{<%= @resource_singular %>.id}" end