silverdr
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.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Since version
1.9.0there is a function for that, see: Decimal.normalize/1silverdr
As you can see above I am quite aware of the existence of this function. The problem is not with “normalising” the number in general but with having the form inputs prepopulated with “normalised” values.
Eiji
Oh, right - I didn’t read the last note, sorry
So the problem is not with
decimalat all …Why you try to access
f.data.field? If I remember correctly there are 2 value maps indataandparamskeys, so if one isnilyou should fetch the other one.Have you tried Phoenix.HTML.Form.input_value instead? It’s doing exactly what I have described above.
silverdr
Well, because I don’t know any better at the moment
This is editing an existing record and I see the values of the record are in fact there but for some not obvious to me yet reason I am unable to use them as argument to functions. I can put them directly as
value: f.data.fieldthough…Heck, I am not even sure if this is what I should be doing as it feels “hacky” to me
Eiji
Since we confirmed it’s not a problem with
decimaldependency there is a very small amount of possible issues in your code:fcould be a wrong variable (rare, but still possible)nilinstead ofdecimalformconstruction behaviour, so saiddecimalshould either be inparamsinstead ofdata(behaviour misunderstanding case) ordatainstead of params` (bug case)If you can confirm (by for example inspecting) that 1 and 2 is not a case then most probably it’s nothing else than what I have described in 3rd point. Whatever is true a dedicated functions like
input_value/2linked above should be preferred instead of accessing astruct’s field directly.Of course it could be other issue not related to 3 points above like incorrectly accessing nested form fields, but this cannot be deducted from what you have shared here.
Edit: While above is still true (in context of my previous message) I have realised that there is some misunderstanding in our talk. First of all
Decimal.normalize/1cannot returnnilvalue! I have confirmed it even by viewing it’s code. It’s either returning a normaliseddecimalor raises anerror, so I guess it’s not related to this line, but there is a bug in other part of your code.I would advise to reproduce your issue by creating a small script file. Here you can find an example
ecto_sqlscript:https://github.com/wojtekmach/mix_install_examples/blob/main/ecto_sql.exs
Using above code add
decimal,phoenix_htmland any other dependency you need and simply copy paste your controller/liveview function body into an example function. Try to debug it yourself withIO.inspect/1orKernel.dbg/1and post it here if needed.silverdr
I highly appreciate your engagement and willingness to help but I probably was not clear or explicit enough because the answers to your questions are in fact up there.
fis correct and holds correctPhoenix.HTML.Formstruct. It works, fields are pre-filled with values taken from the data source (RDBMS)paramsbut again I’ll do the testDecimal.normalize/1cannot return nil value. True. And I didn’t write that it returned nil. I wrote that:Now for the quick “crowbar” test. Let’s put
value: f.nonexistentinto the input line. Result:So the form is correct, the form variable
fis correct. The form struct’sdatacontains thefieldandfieldcontains correct, non-nil value. And yet we are talking about a Schrödinger field’s value. If I don’t touch it - it is there:value: f.data.fieldyields correct valueIf I try to touch it - it disappears:
value: Decimal.normalize(f.data.field)yields:value: Decimal.normalize(input_value(f, :field))yields:even more interesting:
value: [Decimal.normalize(input_value(f, :field))]yields:showing exactly what was supposed to be there…
Now, all of those may be a kind of an unexpected side effect of trying to do it all the wrong way (as my guts try to tell me) so my original question remains:
UPDATE 0: I begin to suspect that this appearing/disappearing value may somehow be caused by
LiveViewrendering the page twice… but even if I find it to be true then I am still unsure if there is no better approach for the problem at handsilverdr
UPDATE 1: Yes, I found the problem causing the Schrödinger value - all my fault. But this was actually a side-thread. The original question – w/o focusing on the errors of my original struggle with
Decimalbeing passed nil on occasions – remains:Anything better than what I came up with?
dimitarvp
What was it?
silverdr
Huh… (coughing, trying to hide the embarrassment)…
The field in question is in
has_manycollection’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 ofeditI 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.Eiji
A bit late reply as I was preparing something … bigger … on devtalk.
So problems with nested form fields … where did I heard about it?
Yeah, that’s definitely a completely different case. Glad you have found the problem. Now let’s see where we are …
If we again talk about
Decimalvalue then you have almost the answer. Why almost? Because it does not accept nil …As you can see the trailing zeros have disappeared and the
nilvalue is easy to take care of as well …That’s said still I recommend to go with a default value like
Decimal.new("0")and useinput_value, so no matter if you are at rendering step (edit page) or are submitting the form you always get a desired or defaultDecimalvalue. This way you do not have to worry aboutnilvalues.