Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use to_form :action as changeset action when passed #171

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading