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
Most Liked
silverdr
Huh… (coughing, trying to hide the embarrassment)…
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
Well… you were indirectly right!
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 ![]()
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 ![]()
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
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 …
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









