nallwhy
How to use AshPhoenix.Form.add_error/3?
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.
Marked As Solved
zachdaniel
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")
Also Liked
zachdaniel
indiosmo
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 \\ [])
Last Post!
zachdaniel
Popular in Questions
Other popular topics
Latest Ash Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security










