I’m trying to use AshPhoenix.Form.add_error/3 to add custom validation errors after running some custom logic, but I’m struggling to figure out how to use it correctly. There are no tests or clear examples available, so I’m experimenting with different approaches, but none seem to work as expected.
Here’s what I’m facing:
• If I try to pass a string as the error, I get a warning saying that Ash.Error.Unknown does not implement AshPhoenix.FormData.Error.
• If I manually create an Ash.Error.Changes.InvalidAttribute and pass it in, it gets wrapped in Ash.Error.Invalid. However, when I call AshPhoenix.Form.errors/3, the error does not show up.
What are you calling add_error/3 with? How did you construct the form?
From looking at the source it seems like it matches on the form type (e.g. %Phoenix.HTML.Form{}) and then on the form source (e.g. %Ash.Changeset{}) and delegates to the source’s add_error.
For a changeset, passing a string should be fine:
@doc """
Adds an error to the changesets errors list, and marks the change as `valid?: false`.
## Error Data
The given `errors` argument can be a string, a keyword list, a struct, or a list of any of the three.
If `errors` is a keyword list, or a list of keyword lists, the following keys are supported in the keyword list:
- `field` (atom) - the field that the error is for. This is required, unless `fields` is given.
- `fields` (list of atoms) - the fields that the error is for. This is required, unless `field` is given.
- `message` (string) - the error message
- `value` (any) - (optional) the field value that caused the error
"""
@spec add_error(t(), error_info() | [error_info()], Keyword.t()) :: t()
@spec add_error(t(), term | String.t() | list(term | String.t())) :: t()
def add_error(changeset, errors, path \\ [])
Great answer! I think likely the issue with it not displaying is around the error not being attached to a field in the form, and for that to happen, the error has to have a field/path attached to it.
The best way to make that happen is to provide a field, and optionally a path if the error is in a nested form. i.e
AshPhoenix.Form.add_error(form, field: :text, path: [:comments, 0], message: "can't do that")
Hi Zach, I’m new to both Ash and Elixir so I might be misunderstanding something, but it looks like the @doc and the name of the arguments for AshPhoenix.Form.add_error/3 do not match what you suggested.
def add_error(form, path, opts \\ [])
def add_error(%Phoenix.HTML.Form{} = form, path, opts) do
def add_error(form, error, opts) do
The second argument being path on the general case and the Phoenix.HTML.Form case and being error in the actual implementation looks confusing, especially since the function taking a path argument as I understand it, simply unwraps the underlying Ash.Form from the Phoenix form.source and delegates to the function taking error.
:path - The path to add the error to. If the error(s) already have a path, don’t specify a path yourself."
But you provided a keyword list, which as I understand it binds to the path/error argument, with opts being empty. Your path: [:comments, 0] being one item in your keyword list, but then the function actually looks for path in opts if opts[:path] do.
So maybe updating the doc/variable names or adding a @spec would help here.
Oops! You’re totally right! Technically a path can be specified in the options to the add_error function, or in the options to create the error itself.
The documentation of Ash.Changeset.add_error was updated recently to link to Ash.Error, so that issue is handled, but I will fix the AshPhoenix.Form error now