nallwhy

nallwhy

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.

Showing Posts 1 to 5

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 \\ [])

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")
indiosmo

indiosmo

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.

The @doc says

: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.

zachdaniel

zachdaniel

Creator of Ash

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 :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews