AshPhoenixForm not executing action on submit

Hey, i'm not sure why but when i submit a form for my update action it is not executing any update query
form = AshPhoenix.Form.for_update(data, :update, actor: actor) |> to_form()
# ....
def handle_event("save", params, socket) do
case AshPhoenix.Form.submit(socket.assigns.form, params: params) do
{:ok, result} ->
{:noreply, put_flash(socket, :info, "successfully saved the changes")}

{:error, _form} ->
{:noreply, put_flash(socket, :error, "failed to save changes")}
end
end
form = AshPhoenix.Form.for_update(data, :update, actor: actor) |> to_form()
# ....
def handle_event("save", params, socket) do
case AshPhoenix.Form.submit(socket.assigns.form, params: params) do
{:ok, result} ->
{:noreply, put_flash(socket, :info, "successfully saved the changes")}

{:error, _form} ->
{:noreply, put_flash(socket, :error, "failed to save changes")}
end
end
Am i missing something? i was following this example https://github.com/team-alembic/realworld/blob/main/lib/realworld_web/live/editor_live/index.ex#L24
GitHub
realworld/lib/realworld_web/live/editor_live/index.ex at main · te...
A fullstack Phoenix LiveView application with backend built with Ash Framework - team-alembic/realworld
Solution:
shouldnt it be like this? ``` def handle_event("save", %{"form" => form_data}, socket) do case AshPhoenix.Form.submit(socket.assigns.form,...
Jump to solution
9 Replies
Ahrou
AhrouOP3mo ago
This is the only thing i see in the logs when submitting
22:59:15.278 [debug] HANDLE EVENT "save" in ConsoleWeb.Live.TwilioNumber
Parameters: %{"form" => %{"callee_status" => "", "campaign_id" => "", "country" => "US", "date" => "", "disabled" => "", "enabled" => "", "locked" => "", "number" => "+16085995475gfd", "organization_id" => "1111", "project_id" => "2222", "site_id" => "", "team_id" => "", "to" => "", "twilio_status" => "true"}}

22:59:15.279 [debug] Replied in 750µs
iex(2)>
22:59:15.278 [debug] HANDLE EVENT "save" in ConsoleWeb.Live.TwilioNumber
Parameters: %{"form" => %{"callee_status" => "", "campaign_id" => "", "country" => "US", "date" => "", "disabled" => "", "enabled" => "", "locked" => "", "number" => "+16085995475gfd", "organization_id" => "1111", "project_id" => "2222", "site_id" => "", "team_id" => "", "to" => "", "twilio_status" => "true"}}

22:59:15.279 [debug] Replied in 750µs
iex(2)>
ZachDaniel
ZachDaniel3mo ago
Try adding 'force?: true' to the submit options By default invalid forms are not submitted So you can check for errors on the form as well I added a new helper AshPhoenix.Form.raw_errors to help debug these kinds of things.
Ahrou
AhrouOP3mo ago
still doesn't submit save and there are no errors 🤔
No description
ZachDaniel
ZachDaniel3mo ago
Add for_path: :all to the raw errors options
Ahrou
AhrouOP3mo ago
still 😅
|> AshPhoenix.Form.raw_errors(for_path: :all) #=> %{}


23:03:45.076 [debug] Replied in 5ms
iex(4)>
|> AshPhoenix.Form.raw_errors(for_path: :all) #=> %{}


23:03:45.076 [debug] Replied in 5ms
iex(4)>
ZachDaniel
ZachDaniel3mo ago
🤔 So it's getting to the error case I assume right? Can is see your full code where you are inspecting those errors?
Ahrou
AhrouOP3mo ago
this should be the full code for that form:
def mount(%{"id" => id}, _session, socket) do
actor = socket.assigns.current_user.actor

data =
Core.TwilioNumber
|> Ash.get!(id, actor: actor)
|> Ash.load!(@preloads, actor: actor)

form = AshPhoenix.Form.for_update(data, :update, actor: actor) |> to_form()

{:ok,
socket
|> assign(data: data, form: form)}
end

def handle_event("validate", params, socket) do
form = AshPhoenix.Form.validate(socket.assigns.form, params)
{:noreply, assign(socket, form: form)}
end

def handle_event("save", params, socket) do
socket.assigns.form
|> AshPhoenix.Form.raw_errors(for_path: :all)
|> dbg

case AshPhoenix.Form.submit(socket.assigns.form, params: params, force?: true) do
{:ok, result} ->
{:noreply, put_flash(socket, :info, "successfully saved the changes")}

{:error, _form} ->
{:noreply, put_flash(socket, :error, "failed to save changes")}
end
end
def mount(%{"id" => id}, _session, socket) do
actor = socket.assigns.current_user.actor

data =
Core.TwilioNumber
|> Ash.get!(id, actor: actor)
|> Ash.load!(@preloads, actor: actor)

form = AshPhoenix.Form.for_update(data, :update, actor: actor) |> to_form()

{:ok,
socket
|> assign(data: data, form: form)}
end

def handle_event("validate", params, socket) do
form = AshPhoenix.Form.validate(socket.assigns.form, params)
{:noreply, assign(socket, form: form)}
end

def handle_event("save", params, socket) do
socket.assigns.form
|> AshPhoenix.Form.raw_errors(for_path: :all)
|> dbg

case AshPhoenix.Form.submit(socket.assigns.form, params: params, force?: true) do
{:ok, result} ->
{:noreply, put_flash(socket, :info, "successfully saved the changes")}

{:error, _form} ->
{:noreply, put_flash(socket, :error, "failed to save changes")}
end
end
and iirc it was getting to the success case that's why i was finding it weird that the changes arent saved and there is no query in the debug logs and this is the form
<%= if Ash.can?({Core.TwilioNumber, :update}, @current_user.actor) do %>
<.card>
<:title>{@data.number}</:title>
<.simple_form for={@form} phx-change="validate" phx-submit="save">
<.input field={@form[:organization_id]} label="organization" />
<.input field={@form[:project_id]} label="project" />
<.input field={@form[:site_id]} label="site" />
<.input field={@form[:team_id]} label="team" />
<.input field={@form[:campaign_id]} label="campaign" />
<.input field={@form[:number]} label="number" />
<.input field={@form[:callee_status]} label="callee_status" />
<.input field={@form[:enabled]} label="enabled" />
<.input field={@form[:disabled]} label="disabled" />
<.input field={@form[:to]} label="to" />
<.input field={@form[:date]} label="date" />
<.input field={@form[:locked]} label="locked" />
<.input field={@form[:country]} label="country" />
<.input field={@form[:twilio_status]} label="twilio_status" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
</.card>
<%= else %>
<%= if Ash.can?({Core.TwilioNumber, :update}, @current_user.actor) do %>
<.card>
<:title>{@data.number}</:title>
<.simple_form for={@form} phx-change="validate" phx-submit="save">
<.input field={@form[:organization_id]} label="organization" />
<.input field={@form[:project_id]} label="project" />
<.input field={@form[:site_id]} label="site" />
<.input field={@form[:team_id]} label="team" />
<.input field={@form[:campaign_id]} label="campaign" />
<.input field={@form[:number]} label="number" />
<.input field={@form[:callee_status]} label="callee_status" />
<.input field={@form[:enabled]} label="enabled" />
<.input field={@form[:disabled]} label="disabled" />
<.input field={@form[:to]} label="to" />
<.input field={@form[:date]} label="date" />
<.input field={@form[:locked]} label="locked" />
<.input field={@form[:country]} label="country" />
<.input field={@form[:twilio_status]} label="twilio_status" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
</.card>
<%= else %>
Solution
schnady
schnady3mo ago
shouldnt it be like this?
def handle_event("save", %{"form" => form_data}, socket) do
case AshPhoenix.Form.submit(socket.assigns.form,
params: form_data
) do
def handle_event("save", %{"form" => form_data}, socket) do
case AshPhoenix.Form.submit(socket.assigns.form,
params: form_data
) do
or
def handle_event("save", params, socket) do
case AshPhoenix.Form.submit(socket.assigns.form,
params: Map.get(params, "form")
) do
def handle_event("save", params, socket) do
case AshPhoenix.Form.submit(socket.assigns.form,
params: Map.get(params, "form")
) do
Ahrou
AhrouOP3mo ago
this was the issue thank you so much 🙏 i was too focused on the save that didnt notice the validate was using the wrong params

Did you find this page helpful?