Skip to content

Commit

Permalink
fix: ensure that errors on before_action hooks invalidate the form
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 11, 2024
1 parent 3ef7acb commit 44437af
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@ defmodule AshPhoenix.Form do
%{form | source: query},
errors
)
|> Map.put(:valid?, false)
|> carry_over_errors()
|> update_all_forms(fn form ->
%{form | just_submitted?: true, submitted_once?: true}
Expand Down Expand Up @@ -2075,6 +2076,7 @@ defmodule AshPhoenix.Form do
errors
)
|> carry_over_errors()
|> Map.put(:valid?, false)
|> update_all_forms(fn form ->
%{form | just_submitted?: true, submitted_once?: true}
end)
Expand Down Expand Up @@ -2105,6 +2107,7 @@ defmodule AshPhoenix.Form do
%{form | source: changeset},
errors
)
|> Map.put(:valid?, false)
|> carry_over_errors()
|> update_all_forms(fn form ->
%{form | just_submitted?: true, submitted_once?: true}
Expand Down Expand Up @@ -2135,7 +2138,8 @@ defmodule AshPhoenix.Form do
form
|> update_all_forms(fn form -> %{form | submitted_once?: true, just_submitted?: true} end)
|> synthesize_action_errors()
|> carry_over_errors()}
|> carry_over_errors()
|> Map.put(:valid?, false)}
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ defmodule AshPhoenix.FormTest do
assert form.valid? == false
end

test "validation errors in before action hooks result in valid? false form" do
form = Form.for_create(Post, :create_with_before_action, domain: Domain)
form = AshPhoenix.Form.validate(form, %{"text" => "text"})
{:error, form} = Form.submit(form, params: %{"text" => "text2"})

refute form.valid?
end

test "blank form values unset - helps support dead view forms" do
form =
Form.for_create(PostWithDefault, :create,
Expand Down
4 changes: 4 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ defmodule AshPhoenix.Test.Post do
end
end

create :create_with_before_action do
change before_action(fn changeset, _ -> Ash.Changeset.add_error(changeset, "nope") end)
end

create :create do
primary?(true)
argument(:author, :map, allow_nil?: true)
Expand Down

0 comments on commit 44437af

Please sign in to comment.