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.
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.
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
- #javascript
- #code-sync
- #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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









