Ash FrameworkAF
Ash Frameworkโ€ข7mo agoโ€ข
13 replies
Steve

AshPhoenix.Form.submit is triggering change function to run twice

Say I have a custom change module, like so:

defmodule MyApp.Accounts.Changes.MyCoolChange do
  use Ash.Resource.Change

  @impl true
  def init(opts) do
    {:ok, opts}
  end

  @impl true
  def change(changeset, _opts, _context) do
    dbg("my cool change")
    changeset
  end
end


When I run this in the iex terminal, the dbg statement only gets output once. That's what I would expect.

Now, when I use it in form using
AshPhoenix.Form
, I get the dbg statement two times when doing Form.submit. I even took out all other code so this was the only thing running. The mount would look like this:

def mount(socket)
  socket =
    socket
    |> assign(form: to_form(MyApp.Domain.to_form_my_action())

  {:noreply, socket}


Inside the my_action action, it would look like this:

actions do
  create :my_action do
    argument :my_arg, :string, allow_nil?: false
    change MyCoolChange
  end
end


Then the event handler looks like this:

def handle_event("submit", %{"form" => form_data}, socket) do
  case Form.submit(socket.assigns.form, params: form_data}) do
    {:ok, result} ->
      # ...

    {:error, form} ->
      # ...
  end
end


With all this in place, the dbg message at the start ("my cool change") ends up being called two times. I can put a dbg in the handle_event and it's only called once. I've checked everywhere along the path and it's during the Form.submit call that it happens two times. Is that expected behavior or a bug?

If I debug inside something like a before_action that is defined in that same change module, the before_action debug only gets called once, same with action_action. But any code inside that change function at the root gets run two times.
Was this page helpful?