This commit is contained in:
Zach Daniel 2022-06-28 22:22:38 -04:00
parent e8bf9d648d
commit 6ba516aeaa
3 changed files with 333 additions and 270 deletions

View file

@ -49,12 +49,11 @@ defmodule AshPhoenix.Form.Auto do
if you are using liveview, you could actually look up the record using the input from the read action, and then use `AshPhoenix.Form.update_form/3`
to set that looked up record as the data of the `_update` form.
### Many to Many Relationshisp
### Many to Many Relationships
In the case that a manage_change option points to a join relationship, that form is presented via a special nested form called
`_join`. So the first form in `inputs_for(form, :relationship)` would be for the destination, and then inside of that you could say
`inputs_for(nested_form, :_join)`. The parameters are merged together during submission.
"""
@dialyzer {:nowarn_function, rel_to_resource: 2}

View file

@ -848,6 +848,9 @@ defmodule AshPhoenix.Form do
new_forms =
Enum.reduce(form_params, forms, fn {index, params}, forms ->
if params["_ignore"] == "true" do
Map.put_new(forms, key, [])
else
case Enum.find(form.forms[key] || [], &matcher.(&1, params, form, key, index)) do
nil ->
create_action =
@ -888,6 +891,7 @@ defmodule AshPhoenix.Form do
[validated]
end)
end
end
end)
if Map.has_key?(new_forms, opts[:as] || key) do
@ -897,6 +901,9 @@ defmodule AshPhoenix.Form do
else
new_forms
end
else
if form_params["_ignore"] == "true" do
Map.put_new(forms, key, nil)
else
if form.forms[key] do
new_form =
@ -929,6 +936,7 @@ defmodule AshPhoenix.Form do
Map.put(forms, key, new_form)
end
end
end
_ ->
case opts[:type] do
@ -2847,6 +2855,7 @@ defmodule AshPhoenix.Form do
transform_errors
) do
if (opts[:type] || :single) == :single do
if map(form_params)["_ignore"] != "true" do
if map(form_params)["_form_type"] == "read" do
read_action =
opts[:read_action] ||
@ -2890,9 +2899,13 @@ defmodule AshPhoenix.Form do
id: id <> "_#{key}"
)
end
end
else
form_params
|> indexed_list()
|> Enum.reject(fn {form_params, _} ->
map(form_params)["_ignore"] == "true"
end)
|> Enum.with_index()
|> Enum.map(fn {{form_params, original_index}, index} ->
if map(form_params)["_form_type"] == "read" do
@ -2965,6 +2978,7 @@ defmodule AshPhoenix.Form do
end
if (opts[:type] || :single) == :single do
if map(form_params)["_ignore"] != "true" do
if data do
case map(form_params)["_form_type"] || "update" do
"update" ->
@ -3051,6 +3065,7 @@ defmodule AshPhoenix.Form do
raise "unexpected form type for form with no data #{other} with params: #{inspect(form_params)}"
end
end
end
else
data = List.wrap(data)
@ -3059,6 +3074,15 @@ defmodule AshPhoenix.Form do
|> Enum.with_index()
|> Enum.reduce({[], List.wrap(data)}, fn {{form_params, original_index}, index},
{forms, data} ->
if form_params["_ignore"] == true do
case data do
[_item | rest] ->
{forms, rest}
_ ->
{forms, data}
end
else
if map(form_params)["_form_type"] == "read" do
resource =
opts[:read_resource] || opts[:resource] ||
@ -3174,6 +3198,7 @@ defmodule AshPhoenix.Form do
{[form | forms], []}
end
end
end
end)
|> elem(0)
|> Enum.reverse()

View file

@ -117,6 +117,45 @@ defmodule AshPhoenix.FormTest do
assert Form.params(form) == %{"comments" => [%{"id" => comment.id}]}
end
test "ignoring a form filters it from the parameters" do
post =
Post
|> Ash.Changeset.new(%{text: "post"})
|> Api.create!()
comment =
Comment
|> Ash.Changeset.new(%{text: "comment"})
|> Ash.Changeset.replace_relationship(:post, post)
|> Api.create!()
form =
post
|> Form.for_update(:update_with_replace,
api: Api,
forms: [
comments: [
read_resource: Comment,
type: :list,
read_action: :read,
data: [comment]
]
]
)
assert [comment_form] = inputs_for(form_for(form, "blah"), :comments)
assert Phoenix.HTML.Form.input_value(comment_form, :text) == "comment"
form = Form.validate(form, %{"comments" => [%{"id" => comment.id, "_ignore" => "true"}]})
assert Form.params(form) == %{"comments" => []}
form = Form.validate(form, %{"comments" => [%{"id" => comment.id, "_ignore" => "false"}]})
assert Form.params(form) == %{"comments" => [%{"id" => comment.id}]}
end
describe "the .changed? field is updated as data changes" do
test "it is false for a create form with no changes" do
form =