elixirmoney / money

Elixir library for working with Money safer, easier, and fun... Is an interpretation of the Fowler's Money pattern in fun.prog.
https://hex.pm/packages/money/
MIT License
827 stars 141 forks source link

Possible Bug with live and validations? #145

Closed tostr7191 closed 1 year ago

tostr7191 commented 4 years ago

Hi

I have created a basic phoenix project, my schema has two integer fields and the schema definition looks like this:

  schema "expenses" do
    field :amount, Money.Ecto.Amount.Type
    field :file_name, :string
    field :telegram_id, :string
    field :telegram_nick, :string
    field :vat_amount, Money.Ecto.Amount.Type

On the default generated views (in my case /expenses/new, any value that I put into one of the Money-fields gets reset to an empty value as soon as I jump to a different field and trigger validation there. Below is the screenshot after I have input "1234" into "Amount" then TAB to jump to "Vat Amount" and as soon as I input 1 into "Vat Amount" the value in "Amount" gets set to empty and the validation (can't be blank) shows.

Screenshot 2020-07-30 at 14 33 36

I have recreated this in a fresh project with nothing added except for:

mix phx.gen.live Tally Expense expenses telegram_id telegram_nick file_name amount:integer vat_amount:integer

I have to admit that I am very new to phoenix, it might be that I am missing something obvious here.

Edit: If I remove phx_change: "validate", from lib/testme_web/live/expense_live/form_component.html.leex it works, gets saved to database correctly and also renders correctly. It's just the validation that does for some reason throws away the data sometimes.

BartDeCaluwe commented 3 years ago

For anyone having the same issue, this is what fixed it for me:

instead of defining the form field like this:

 <%= text_input f, :amount, type: "number", placeholder: "0" %>

change the type from "number" to "text" like this:

 <%= text_input f, :amount, type: "text", placeholder: "0" %>

You can also just omit type: "text" as it is the default type of a text_input.

atavistock commented 2 years ago

Seems like this should be closed.