Skip to content

Commit

Permalink
Do not override changeset action
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 17, 2024
1 parent 0841557 commit bb3504e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/phoenix_ecto/html.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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
%{params: params, data: data, action: action} = changeset
{action, opts} = Keyword.pop(opts, :action, action)
{name, opts} = Keyword.pop(opts, :as)

name = to_string(name || form_for_name(data))
Expand Down
5 changes: 2 additions & 3 deletions test/phoenix_ecto/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ defmodule PhoenixEcto.HTMLTest do
assert f.source.action == nil
end

test "to_form :action option sets changeset action" do
test "to_form :action option sets form action" do
changeset =
%User{}
|> cast(%{}, ~w()a)
|> validate_length(:name, min: 3)
|> Map.put(:action, :validate)
|> Map.replace!(:action, :validate)

assert changeset.action == :validate
f = to_form(changeset, action: :insert)
assert f.action == :insert
assert f.source.action == :insert
end

test "to_form without :action option uses changeset action" do
Expand Down

0 comments on commit bb3504e

Please sign in to comment.