Skip to content

Commit

Permalink
improvement: add add_error/3
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jan 22, 2024
1 parent 36df5a4 commit 0f3a0c7
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1757,13 +1757,13 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do
raise Ash.Error.to_error_class(query.errors, query: query)
else
query = query || original_changeset_or_query

errors =
error
|> List.wrap()
|> Enum.flat_map(&expand_error/1)

query = query || %{original_changeset_or_query | errors: errors}

{:error,
set_action_errors(
%{form | source: query},
Expand All @@ -1780,13 +1780,13 @@ defmodule AshPhoenix.Form do
if opts[:raise?] do
raise Ash.Error.to_error_class(changeset.errors, changeset: changeset)
else
changeset = changeset || original_changeset_or_query

errors =
error
|> List.wrap()
|> Enum.flat_map(&expand_error/1)

changeset = changeset || %{original_changeset_or_query | errors: errors}

{:error,
set_action_errors(
%{form | source: changeset},
Expand Down Expand Up @@ -2761,6 +2761,51 @@ defmodule AshPhoenix.Form do
]
]

@doc """
Adds an error to the source underlying the form.
This can be used for adding errors from different sources to a form. Keep in mind, if they don't match
a field on the form (typically extracted via the `field` key in the error), they won't be displayed by default.
# Options
- `:path` - The path to add the error to. If the error(s) already have a path, don't specify a path yourself.
"""
def add_error(form, path, opts \\ [])

def add_error(%Phoenix.HTML.Form{} = form, path, opts) do
form.source
|> add_error(path, opts)
|> Phoenix.HTML.FormData.to_form(form.options)
end

def add_error(form, error, opts) do
error = Ash.Error.to_error_class(error)

error =
if opts[:path] do
Ash.Error.set_path(error, opts[:path])
else
error
end

new_source =
case form.source do
%Ash.Query{} ->
Ash.Query.add_error(form.source, error)

%Ash.Changeset{} ->
Ash.Changeset.add_error(form.source, error)

%Ash.ActionInput{} ->
Ash.ActionInput.add_error(form.source, error)
end

%{form | source: new_source}
|> set_validity()
|> carry_over_errors()
end

@doc """
Adds a new form at the provided path.
Expand Down

0 comments on commit 0f3a0c7

Please sign in to comment.