nallwhy

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

zachdaniel

Creator of Ash

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

indiosmo

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!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement