Ash FrameworkAF
Ash Framework7mo ago
10 replies
Aron Zwaan

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

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

which, admittedly, is slightly complicated.

Does anyone have a clue what might be going wrong here?
Was this page helpful?