fix: properly synthesize nested action errors

This commit is contained in:
Zach Daniel 2022-09-28 02:11:42 -04:00
parent 6c6c2e5fff
commit 6759794d60

View file

@ -2871,30 +2871,47 @@ defmodule AshPhoenix.Form do
} }
end end
defp synthesize_action_errors(form, trail \\ []) do defp synthesize_action_errors(form, trail \\ [], further_errors \\ []) do
errors =
form.source.errors
|> List.wrap()
|> Enum.flat_map(&expand_error/1)
|> Enum.map(fn error ->
%{error | path: trail ++ error.path}
end)
further_errors = further_errors ++ Enum.reject(errors, &(&1.path == trail))
new_forms = new_forms =
form.forms form.forms
|> Map.new(fn {key, forms} -> |> Map.new(fn {key, forms} ->
config = form.form_keys[key]
new_forms = new_forms =
if is_list(forms) do if is_list(forms) do
Enum.map(forms, fn form -> forms
synthesize_action_errors(form, [key | trail]) |> Enum.with_index()
|> Enum.map(fn {form, index} ->
synthesize_action_errors(
form,
trail ++ [config[:for] || key, index],
further_errors
)
end) end)
else else
if forms do if forms do
synthesize_action_errors(forms, [key | trail]) synthesize_action_errors(forms, trail ++ [config[:for] || key], further_errors)
end end
end end
{key, new_forms} {key, new_forms}
end) end)
errors = %{
form.source.errors form
|> List.wrap() | submit_errors: transform_errors(form, errors ++ further_errors, trail, form.form_keys),
|> Enum.flat_map(&expand_error/1) forms: new_forms
}
%{form | submit_errors: transform_errors(form, errors, [], form.form_keys), forms: new_forms}
end end
defp expand_error(%class_mod{} = error) defp expand_error(%class_mod{} = error)