Handling an %Ash.Error.Invalid{} in LiveView

Sorry for the noob question, but the AshPhoenix guide doesn't show any error handling. This is the code that I assumed would work, but update returns a {:error, %Ash.Error.Invalid{} } How do I get those errors onto the form?
def handle_event("update-slug", params, socket) do
current_account = socket.assigns.current_account

case Account.update(current_account, params, actor: socket.assigns.current_user) do
{:ok, applied_account} ->
info = "Slug changed successfully."

socket =
socket
|> put_flash(:info, info)
|> assign(:current_account, applied_account)
|> push_navigate(to: ~p"/#{applied_account.slug}/settings")

{:noreply, socket}

{:error, slug_form} ->
{:noreply, assign(socket, :slug_form, slug_form)}
end
end
def handle_event("update-slug", params, socket) do
current_account = socket.assigns.current_account

case Account.update(current_account, params, actor: socket.assigns.current_user) do
{:ok, applied_account} ->
info = "Slug changed successfully."

socket =
socket
|> put_flash(:info, info)
|> assign(:current_account, applied_account)
|> push_navigate(to: ~p"/#{applied_account.slug}/settings")

{:noreply, socket}

{:error, slug_form} ->
{:noreply, assign(socket, :slug_form, slug_form)}
end
end
7 Replies
ZachDaniel
ZachDaniel3y ago
You should use AshPhoenix.Form to get good error handling in forms Have you looked through the examples of how to use AshPhoenix.Form? A recent good example project using them was shared in the #showcase channel
Robert Graff
Robert GraffOP3y ago
Looking now...
ZachDaniel
ZachDaniel3y ago
Did you create a form for the update slug work that you're doing? Because your handle_event there should look something like this if implemented correctly:
case AshPhoenix.Form.submit(form, params: params) do
{:ok, account} ->
...
{:error, form} ->
{:noreply, assign(socket, :slug_form, slug_for)}
end
case AshPhoenix.Form.submit(form, params: params) do
{:ok, account} ->
...
{:error, form} ->
{:noreply, assign(socket, :slug_form, slug_for)}
end
Robert Graff
Robert GraffOP3y ago
I created a form like this...
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
user = socket.assigns.current_user
current_account = socket.assigns.current_account

settings_form = AshPhoenix.Form.for_update(current_account, :update) |> to_form()
slug_form = AshPhoenix.Form.for_update(current_account, :update) |> to_form()

socket =
socket
|> assign(:current_user, user)
|> assign(:header, "Settings")
|> assign(:account_slug, current_account.slug)
|> assign(:current_uri, ~p"/#{current_account.slug}/settings")
|> assign(:settings_form, settings_form)
|> assign(:slug_form, slug_form)

{:ok, socket}
end
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
user = socket.assigns.current_user
current_account = socket.assigns.current_account

settings_form = AshPhoenix.Form.for_update(current_account, :update) |> to_form()
slug_form = AshPhoenix.Form.for_update(current_account, :update) |> to_form()

socket =
socket
|> assign(:current_user, user)
|> assign(:header, "Settings")
|> assign(:account_slug, current_account.slug)
|> assign(:current_uri, ~p"/#{current_account.slug}/settings")
|> assign(:settings_form, settings_form)
|> assign(:slug_form, slug_form)

{:ok, socket}
end
ZachDaniel
ZachDaniel3y ago
Gotcha, so now you need to submit that form What you're currently doing is getting the current account and calling the update logic on it directly but you should use AshPhoenix.Form.submit(socket.assigns.slug_form, params: params) to submit it then you'd get {:error, form} back from the error case
Robert Graff
Robert GraffOP3y ago
Got it Thanks for the help!
ZachDaniel
ZachDaniel3y ago
Happy to help!

Did you find this page helpful?