Skip to content

Commit

Permalink
improvement: properly get invalid values provided to forms
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Nov 28, 2023
1 parent 560fa66 commit 6ddddf7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,8 @@ defmodule AshPhoenix.Form do
end

defp do_value(%{source: %Ash.Changeset{} = changeset} = form, field) do
with :error <- get_changing_value(changeset, field),
with :error <- get_invalid_value(changeset, field),
:error <- get_changing_value(changeset, field),
:error <- Ash.Changeset.fetch_argument(changeset, field),
:error <- get_non_attribute_non_argument_param(changeset, form, field),
:error <- Map.fetch(changeset.data, field) do
Expand Down Expand Up @@ -2378,6 +2379,26 @@ defmodule AshPhoenix.Form do
end
end

defp get_invalid_value(changeset, field) when is_atom(field) do
if field in changeset.invalid_keys do
with :error <- Map.fetch(changeset.params, field) do
Map.fetch(changeset.params, to_string(field))
end
else
:error
end
end

defp get_invalid_value(changeset, field) when is_binary(field) do
if Enum.any?(changeset.invalid_keys, &(to_string(&1) == field)) do
with :error <- Map.fetch(changeset.params, field) do
Map.fetch(changeset.params, to_string(field))
end
else
:error
end
end

defp get_changing_value(changeset, field) do
Map.fetch(changeset.attributes, field)
end
Expand Down
10 changes: 10 additions & 0 deletions test/form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ defmodule AshPhoenix.FormTest do

assert AshPhoenix.Form.value(form, :password) == "fo"
end

test "lists with invalid values return those invalid values when getting them" do
form =
Post
|> Form.for_create(:create_author_required, api: Api, forms: [auto?: true])
|> Form.validate(%{"list_of_ints" => %{"0" => %{"map" => "of stuff"}}})

# TODO: this might be wrong
assert AshPhoenix.Form.value(form, :list_of_ints) == %{"0" => %{"map" => "of stuff"}}
end
end

describe "form_for fields" do
Expand Down
1 change: 1 addition & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule AshPhoenix.Test.Post do
attribute(:text, :string, allow_nil?: false)
attribute(:union, AshPhoenix.Test.UnionValue)
attribute(:union_array, {:array, AshPhoenix.Test.UnionValue})
attribute(:list_of_ints, {:array, :integer})
attribute(:title, :string)
end

Expand Down

0 comments on commit 6ddddf7

Please sign in to comment.