Skip to content

Commit

Permalink
Merge pull request #171 from phoenixframework/cm-form-action
Browse files Browse the repository at this point in the history
Use to_form :action as changeset action when passed
  • Loading branch information
chrismccord committed Feb 29, 2024
2 parents 223bdb6 + 0ea46ed commit 89a92b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/phoenix_ecto/html.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
if Code.ensure_loaded?(Phoenix.HTML) do
defimpl Phoenix.HTML.FormData, for: Ecto.Changeset do
def to_form(changeset, opts) do
{action, opts} = Keyword.pop_lazy(opts, :action, fn -> changeset.action end)
changeset = Map.put(changeset, :action, action)

%{params: params, data: data} = changeset
{name, opts} = Keyword.pop(opts, :as)

Expand All @@ -11,16 +14,17 @@ if Code.ensure_loaded?(Phoenix.HTML) do
source: changeset,
impl: __MODULE__,
id: id,
action: action,
name: name,
errors: form_for_errors(changeset),
errors: form_for_errors(changeset, action),
data: data,
params: params || %{},
hidden: form_for_hidden(data),
options: Keyword.put_new(opts, :method, form_for_method(data))
}
end

def to_form(%{action: parent_action} = source, form, field, opts) do
def to_form(source, %{action: parent_action} = form, field, opts) do
if Keyword.has_key?(opts, :default) do
raise ArgumentError,
":default is not supported on inputs_for with changesets. " <>
Expand Down Expand Up @@ -58,7 +62,7 @@ if Code.ensure_loaded?(Phoenix.HTML) do
impl: __MODULE__,
id: id,
name: name,
errors: form_for_errors(changeset),
errors: form_for_errors(changeset, parent_action),
data: data,
params: params || %{},
hidden: form_for_hidden(data),
Expand Down Expand Up @@ -92,7 +96,7 @@ if Code.ensure_loaded?(Phoenix.HTML) do
id: id <> "_" <> index_string,
name: name <> "[" <> index_string <> "]",
index: index,
errors: form_for_errors(changeset),
errors: form_for_errors(changeset, parent_action),
data: data,
params: params || %{},
hidden: form_for_hidden(data),
Expand Down Expand Up @@ -302,9 +306,9 @@ if Code.ensure_loaded?(Phoenix.HTML) do
raise ArgumentError, "expected #{what} to be a map/struct, got: #{inspect(value)}"
end

defp form_for_errors(%{action: nil}), do: []
defp form_for_errors(%{action: :ignore}), do: []
defp form_for_errors(%{errors: errors}), do: errors
defp form_for_errors(_changeset, nil = _action), do: []
defp form_for_errors(_changeset, :ignore = _action), do: []
defp form_for_errors(%Ecto.Changeset{errors: errors}, _action), do: errors

defp form_for_hidden(%{__struct__: module} = data) do
module.__schema__(:primary_key)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule PhoenixEcto.Mixfile do

defp deps do
[
{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", optional: true},
{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", optional: true},
{:ecto, "~> 3.5"},
{:plug, "~> 1.9"},
{:ex_doc, ">= 0.0.0", only: :docs}
Expand Down

0 comments on commit 89a92b2

Please sign in to comment.