carlgleisner

carlgleisner

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:

Showing Posts 1 to 4

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:

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…

carlgleisner

carlgleisner OP

Thank you for taking the time to mention transform_errors! I tried to simplify my post by leaving out the transform_errors calls I already use for localising my custom Ash resource validations.

Btw, obviously not Ash specific but I wanted this to show up for any future community members searching for this or a similar issue.

The GitHub issue in LiveView’s repo that @Wigny linked to is rather spot on for this.

carlgleisner

carlgleisner OP

Many thanks @Wigny!

This was really spot on, and far more insightful than my own look into the matter. I’m glad that you opened the issue and I read it with great interest.

Will subscribe to any updates. I guess it would be practical for the community to record any resolution in this thread as well :pray:

— 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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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