silverdr

silverdr

Preprocessing values for inputs in a Phoenix (LiveView) form

I have a (LiveView) form with some inputs for fields, which are backed by relatively large precision/scale decimals. The problem I have is that when editing existing record, the inputs

<%= number_input f, :field, ... %>

get prepopulated with values taken from the database at its full scale:

Decimal.new("123.45000000")

and the input is then pre-filled with 123.45000000

What would be the suggested way for getting rid of all the trailing zeroes?

For testing I tried adding “value” attribute to the input but whenever I try to process something like Decimal.normalize(f.data.field) I get nil passed to normalize/1 instead of the number. Just giving f.data.field (or Decimal.normalize(Decimal.new("123.45000000")) for value works OTOH.

First Post!

Eiji

Eiji

Since version 1.9.0 there is a function for that, see: Decimal.normalize/1

Most Liked

silverdr

silverdr

Huh… (coughing, trying to hide the embarrassment)… :face_with_open_eyes_and_hand_over_mouth: The field in question is in has_many collection’s items, where at least one is required to be present. On editing existing records, there should be only valid items inside that collection but for the LiveView render of edit I was adding an empty (hence invalid) one, as if the whole record was new rather than existing/validated/persisted. This was obviously causing all the “weird”, while in fact completely correct, behaviour I mentioned above.

silverdr

silverdr

Well… you were indirectly right! :smiley: Although not exactly a nested form’s problem but rather me adding an unwanted/unexpected collection member where it wasn’t meant to be. And masquerading this fact with some crashes :tired_face:

I use Decimal functions in many places. It’s not about this. It was a side-thread caused by the temporarily hard to explain errors I encountered.

It is more about “how to handle such cases”. I could come up with:

  • building a separate “read” model and transforming (normalising) data on reading from DB
  • doing the normalising in the end on the inputs themselves

The first feels like an overkill for such relatively small problem to solve, while the latter feels a bit “hacky”, like hammering the transformed values into places against the framework’s will :wink:

All that while doubting that my case is a very rare, isolated one and wondering if there is maybe already something built-in or a de facto standard Elixir/Ecto/Phoenix way of dealing with this kind of problems. Something I am simply not yet aware of.

I currently use input_value but having any default there doesn’t make much sense in the case at hand so I handle nil gracefully instead.

Last Post!

Eiji

Eiji

I wound focus on thinking in different direction … If it was not for that case then why Decimal.normalise/1 has been added? Look that you can write a custom Ecto.Type that would do 1:1 of the Decimal stuff, but when it’s about to read a value from database you do again the same thing + normalisation. Whatever you choose is rather up to you. Such specific use cases rather don’t have a good practices guides.

I would rather look at your own code and decide what’s the best way comparing to overall style of your app. For example you may already have 1 or 2 custom Ecto.Type, so introducing another one would be relatively to your code style absolutely normal.

I also haven’t tried it, but there are other PostgreSQL-related solutions like selecting a column and cast it to real or converting a whole column to real type while working on Decimal at Elixir app side. As said I haven’t tested it, so I have no idea about performance, preserving precision and so on …

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement