Ash FrameworkAF
Ash Framework6mo ago
4 replies
Ahrou

How to revalidate a nested AshPhoenix.Form update from an handle_event

I have this handle_event to update the value of a single nested for which has buttons to reset the input/set 50%/set100%:
def handle_event(
        "set_text",
        %{"form_path" => form_path, "field_name" => field_name, "text" => text},
        socket
      ) do
    text = String.split(text, ".") |> Enum.at(0)

    # from https://elixirforum.com/t/update-fields-of-a-nested-ashphoenix-form-on-change-of-a-different-field/60954/2?u=diogo-felix-martins
    # form =
    #   AshPhoenix.Form.update_form(socket.assigns.form, form_path, fn nested_form ->
    #     params = Map.merge(nested_form.params, Map.put(nested_form.params, field_name, text))
    #     AshPhoenix.Form.validate(nested_form, params)
    #   end)

    form =
      AshPhoenix.Form.update_form(socket.assigns.form, form_path, fn nested_form ->
        AshPhoenix.Form.update_params(nested_form, fn new_nested_form ->
          Map.put(new_nested_form, field_name, text)
        end)
      end)

    # need to call validate manually?
    form_params = AshPhoenix.Form.params(form)
    form = AshPhoenix.Form.validate(form, form_params)
    {:noreply, assign(socket, :form, form)}
  end


Feels like i am doing too much, is the the update_params function not necessary?
Do I need to manually call validate or is there some opt that i am missing?
Solution
That looks like how you'd do it currently to me
Was this page helpful?