diff --git a/lib/ash_phoenix/form/form.ex b/lib/ash_phoenix/form/form.ex index 8d29ece..9b8c0f8 100644 --- a/lib/ash_phoenix/form/form.ex +++ b/lib/ash_phoenix/form/form.ex @@ -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} @@ -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) @@ -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} @@ -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 diff --git a/test/form_test.exs b/test/form_test.exs index f07ee18..4c00d57 100644 --- a/test/form_test.exs +++ b/test/form_test.exs @@ -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, diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index d9466c7..c0b6731 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -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)