From 59fc8ffbb009cf590366f7713e89e9d9c9d6216f Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Mon, 5 Feb 2024 14:41:48 -0500 Subject: [PATCH 1/4] Use to_form :action as changeset action when passed --- lib/phoenix_ecto/html.ex | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/phoenix_ecto/html.ex b/lib/phoenix_ecto/html.ex index 65cada0..f7e9543 100644 --- a/lib/phoenix_ecto/html.ex +++ b/lib/phoenix_ecto/html.ex @@ -1,6 +1,12 @@ if Code.ensure_loaded?(Phoenix.HTML) do defimpl Phoenix.HTML.FormData, for: Ecto.Changeset do def to_form(changeset, opts) do + {action, changeset} = + case Keyword.fetch(opts, :action) do + {:ok, action} -> {action, Map.put(changeset, :action, action)} + :error -> {nil, changeset} + end + %{params: params, data: data} = changeset {name, opts} = Keyword.pop(opts, :as) @@ -11,6 +17,7 @@ if Code.ensure_loaded?(Phoenix.HTML) do source: changeset, impl: __MODULE__, id: id, + action: action, name: name, errors: form_for_errors(changeset), data: data, From 8ef8ee83cf3ea5775462ce7185fb24383f3bb2cf Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Tue, 6 Feb 2024 10:17:59 -0500 Subject: [PATCH 2/4] Bump --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 1ef0e6d..ef595db 100644 --- a/mix.exs +++ b/mix.exs @@ -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} From a9e1a3d983042898a37d5aff737d44094b46b4cd Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Tue, 6 Feb 2024 11:26:31 -0500 Subject: [PATCH 3/4] Use changeset action if no action passed --- lib/phoenix_ecto/html.ex | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/phoenix_ecto/html.ex b/lib/phoenix_ecto/html.ex index f7e9543..7099f9f 100644 --- a/lib/phoenix_ecto/html.ex +++ b/lib/phoenix_ecto/html.ex @@ -1,11 +1,8 @@ if Code.ensure_loaded?(Phoenix.HTML) do defimpl Phoenix.HTML.FormData, for: Ecto.Changeset do def to_form(changeset, opts) do - {action, changeset} = - case Keyword.fetch(opts, :action) do - {:ok, action} -> {action, Map.put(changeset, :action, action)} - :error -> {nil, changeset} - end + {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) @@ -19,7 +16,7 @@ if Code.ensure_loaded?(Phoenix.HTML) do 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), @@ -27,7 +24,7 @@ if Code.ensure_loaded?(Phoenix.HTML) do } 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. " <> @@ -65,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), @@ -99,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), @@ -309,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(%Ecto.Changeset{}, nil = _action), do: [] + defp form_for_errors(%Ecto.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) From 0ea46ed25e422c17651d4c9f247b2944447e1177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Feb 2024 18:07:08 +0100 Subject: [PATCH 4/4] Update lib/phoenix_ecto/html.ex --- lib/phoenix_ecto/html.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/phoenix_ecto/html.ex b/lib/phoenix_ecto/html.ex index 7099f9f..305dbde 100644 --- a/lib/phoenix_ecto/html.ex +++ b/lib/phoenix_ecto/html.ex @@ -306,8 +306,8 @@ if Code.ensure_loaded?(Phoenix.HTML) do raise ArgumentError, "expected #{what} to be a map/struct, got: #{inspect(value)}" end - defp form_for_errors(%Ecto.Changeset{}, nil = _action), do: [] - defp form_for_errors(%Ecto.Changeset{}, :ignore = _action), do: [] + 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