Update Params in Nested Form with Union type

I have a code snippet that looks as follows:
def update_subform_field(root_form, subform_name, new_value) do
AshPhoenix.Form.update_form(root_form, subform_name, fn sub_form ->
AshPhoenix.Form.update_params(sub_form, fn current_params ->
Map.put(current_params, :my_field, new_value)
end, only_touched?: true)
end)
end
def update_subform_field(root_form, subform_name, new_value) do
AshPhoenix.Form.update_form(root_form, subform_name, fn sub_form ->
AshPhoenix.Form.update_params(sub_form, fn current_params ->
Map.put(current_params, :my_field, new_value)
end, only_touched?: true)
end)
end
The given values are ["A"] or ["A", "B"]. Using IO.inspect, I can see that the Map.put call works properly. However, in the result of the update_params call, the raw_params field is updated, but the params field is not. Removing the only_touched?: true causes an error during form validation (see stacktrace in thread). To me this suggests that it is a validation error someway. The my_field attribute is defined as
attribute :my_field, :union,
allow_nil?: true,
public?: true,
constraints: [
types: [
%{:string, %{type: :string}},
%{:string_array, %{type: {:array, :string}}},
# ...
]
],
default: nil
attribute :my_field, :union,
allow_nil?: true,
public?: true,
constraints: [
types: [
%{:string, %{type: :string}},
%{:string_array, %{type: {:array, :string}}},
# ...
]
],
default: nil
which, admittedly, is slightly complicated. Does anyone have a clue what might be going wrong here?
5 Replies
Aron Zwaan
Aron ZwaanOP•3mo ago
Form validation error stacktrace:
[phoenix ] [error] GenServer #PID<0.9095.0> terminating
[phoenix ] ** (BadMapError) expected a map, got: nil
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:1350: anonymous fn/1 in AshPhoenix.Form.validate/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:3837: AshPhoenix.Form.update_all_forms/2
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:3848: anonymous fn/2 in AshPhoenix.Form.update_all_forms/2
[phoenix ] (elixir 1.17.3) lib/map.ex:257: Map.do_map/2
[phoenix ] (elixir 1.17.3) lib/map.ex:251: Map.new_from_map/2
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (elixir 1.17.3) lib/list.ex:1351: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/list.ex:1355: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2599: AshPhoenix.Form.update_form/4
[phoenix ] (elixir 1.17.3) lib/list.ex:1351: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2599: AshPhoenix.Form.update_form/4
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2573: AshPhoenix.Form.update_form/4
[phoenix ] [error] GenServer #PID<0.9095.0> terminating
[phoenix ] ** (BadMapError) expected a map, got: nil
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:1350: anonymous fn/1 in AshPhoenix.Form.validate/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:3837: AshPhoenix.Form.update_all_forms/2
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:3848: anonymous fn/2 in AshPhoenix.Form.update_all_forms/2
[phoenix ] (elixir 1.17.3) lib/map.ex:257: Map.do_map/2
[phoenix ] (elixir 1.17.3) lib/map.ex:251: Map.new_from_map/2
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (elixir 1.17.3) lib/list.ex:1351: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/list.ex:1355: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2599: AshPhoenix.Form.update_form/4
[phoenix ] (elixir 1.17.3) lib/list.ex:1351: List.do_update_at/3
[phoenix ] (elixir 1.17.3) lib/map.ex:916: Map.update!/3
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2599: AshPhoenix.Form.update_form/4
[phoenix ] (ash_phoenix 2.1.26) lib/ash_phoenix/form/form.ex:2573: AshPhoenix.Form.update_form/4
ZachDaniel
ZachDaniel•3mo ago
Can you make sure you're on the latest ash_phoenix version? And really all packages
Aron Zwaan
Aron ZwaanOP•3mo ago
We're working on that; might try whether I can merge that in 🙂 Tested with current versions, same behavior
ZachDaniel
ZachDaniel•3mo ago
😢 Can you make a reproduction and open an issue?
Aron Zwaan
Aron ZwaanOP•3mo ago
Sure! Will do tomorrow though, already late here 🙂

Did you find this page helpful?