carlgleisner

carlgleisner

AshMoney and Phoenix inputs - how to show validation errors?

Dear community :waving_hand:t2:

I have an Ash resource with money attributes using ash_money, a thin wrapper around ex_money. I’ve been looking into the question of Phoenix inputs for money attributes and I think that there may be an opportunity for value to the community in large here.

The “tricky” part here is that the Money type is a composite of:

  • amount and
  • currency.

I have managed to dig up a Discord message between @zachdaniel and another user regarding this very issue and that was immensely useful (usually is :slightly_smiling_face:), see the code (completely) inspired by Zach’s response in the mentioned exchange below.

I’m looking into the issue of showing validation errors currently. However, I would think that this is an issue that could be generalised by better skilled people. This should be a really common use case since… everything comes down to money in the end :man_shrugging:t3:

The resource

attributes do
    # yada yada
    attribute :amount, :money, allow_nil?: false, public?: true
end

The inputs

There are two inputs for the attribute, one for the amount and another for the currency.

Note how the value attribute is extracted with helper functions.

<.input
  id={@form.id <> "_amount_amount"}
  name={@form.name <> "[amount][amount]"}
  value={amount(@form[:amount].value)}
  type="number"
  label={gettext("Amount")}
/>

<.input
  id={@form.id <> "_amount_currency"}
  name={@form.name <> "[amount][currency]"}
  value={currency(@form[:amount].value)}
  type="select"
  options={Money.Currency.known_current_currencies()}
  value="EUR"
  label={gettext("Currency")}
/>

The code

The amount and currency values are extracted using the helper functions amount/1 and currency/1 respectively.

defp currency(%{currency: currency}), do: currency
defp currency(%{"currency" => currency}), do: currency

defp currency(_value), do: nil

defp amount(%{amount: amount}), do: amount
defp amount(%{"amount" => amount}), do: amount

defp amount(_), do: nil

A general solution to a general problem?

@zachdaniel mentioned this in the subsequent message in the exchange I linked to above:

I think we need to improve the story for this kind of thing in AshPhoenix , will need to do some thinking on it. But the above will work though

There are other ways to work around these kinds of limitations, so if you get in a pickle what you can do is basically modify the params before they get into your form, and assign some values to correspond to the value.

How would you go about showing errors?

While the big brains ponder the elegant and general solutions to this, I – and I like think others as well – would be very happy to hear any thoughts on how you would handle showing the validation errors for :amount and :currency in the example above :slightly_smiling_face:

Thank you in advance and have a nice day :folded_hands:t3:

Most Liked

Wigny

Wigny

A while ago I added a similar solution of a “composite input” component (updated gist) in this discussion in the ex_money repo, but I also noticed that the error feedback experience was not that great when using this component in forms (see this issue))… :confused:

So I’m also looking for some nice solution to this problem…

zachdaniel

zachdaniel

Creator of Ash

Something to try is adding the transform_errors option when creating the form. You can then check the path and field of each error and rewrite it to be a field in the form. Not super helpful but I’m not at a computer and it may be enough to get you on the right track :slight_smile:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

We're in Beta

About us Mission Statement