Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation breaks Money in Phoenix 1.7 #237

Open
maxgronlund opened this issue Dec 7, 2024 · 0 comments
Open

Validation breaks Money in Phoenix 1.7 #237

maxgronlund opened this issue Dec 7, 2024 · 0 comments

Comments

@maxgronlund
Copy link

Issue with Price Field in LiveView Form

Thanks for the great Money package!

I have a price field declared like this:

schema "product" do
  field :price, Money.Ecto.Amount.Type
  field :some_other_field, :integer
end

I also used the validator from the documentation.

In the LiveView's form component, I'm using <.simple_form>. To get the input to display anything at all, I updated the input like this:

<.input
  field={@form[:price]}
  value={money_to_decimal(Phoenix.HTML.Form.input_value(@form, :price))}
  type="number"
  label="Price"
  step="0.5"
/>

I also created this helper function to avoid crashes when the price is invalid:

defp money_to_decimal(input_value) do
  case input_value do
    %Money{} -> Money.to_decimal(input_value)
    _ -> input_value
  end
end

However, I've discovered an issue: when I update some_other_field twice (e.g., by selecting it in the UI and pressing the "up" arrow key twice), the price starts increasing by a factor of 100.

I hope my explanation is clear. If not, feel free to reach out! I'd love to help resolve this and improve the experience. I think Money is a fantastic Hex package that everyone should use.

Best regards,
Max

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant