fix: check for values in changeset params

This commit is contained in:
Zach Daniel 2023-09-27 18:56:55 -04:00
parent 693ff1108c
commit 91218dd24e
3 changed files with 40 additions and 1 deletions

View file

@ -2383,7 +2383,10 @@ defmodule AshPhoenix.Form do
defp get_non_attribute_non_argument_param(changeset, form, field) do
if Ash.Resource.Info.attribute(changeset.resource, field) ||
Enum.any?(changeset.action.arguments, &(&1.name == field)) do
:error
with :error <- Map.fetch(changeset.params, field),
:error <- Map.fetch(changeset.params, to_string(field)) do
:error
end
else
Map.fetch(AshPhoenix.Form.params(form), Atom.to_string(field))
end

View file

@ -156,6 +156,19 @@ defmodule AshPhoenix.FormTest do
end
end
describe "validations and form values" do
test "validation errors don't clear fields" do
form =
AshPhoenix.Test.User
|> AshPhoenix.Form.for_create(:register)
|> AshPhoenix.Form.validate(%{"password" => "f"})
|> AshPhoenix.Form.validate(%{"password" => "fo"})
|> AshPhoenix.Form.validate(%{"password" => "fo", "password_confirmation" => "foo"})
assert AshPhoenix.Form.value(form, :password) == "fo"
end
end
describe "form_for fields" do
test "it should show simple field values" do
form =

View file

@ -0,0 +1,23 @@
defmodule AshPhoenix.Test.User do
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
attributes do
uuid_primary_key(:id)
attribute(:email, :string, allow_nil?: false)
end
actions do
defaults([:create, :read, :update])
create :register do
argument :password, :string, allow_nil?: false, constraints: [min_length: 12]
argument :password_confirmation, :string, allow_nil?: false, constraints: [min_length: 12]
validate confirm(:password, :password_confirmation)
end
end
end