Kurisu

Kurisu

How to validate that a decimal number is an integer in an Ecto changeset?

I’m using the Decimal package to work with a field in my changeset.

field :amount, :decimal

I can validate it with validate_number so that it should be greater or equal to a given number.

But for now I would like the user to submit an integer for this field (:amount).
I do not want to change the Ecto type to :integer but just want to have a decimal number with zero as decimal part.

Please any suggestion on how to achieve this with a custom Ecto validator, given that the field is of type :decimal?

Marked As Solved

Aetherus

Aetherus

Maybe you can check if it equals to the rounded version of itself.

defp validate_integer(changeset, field) do
  if integer?(get_field(changeset, field)) do
    changeset
  else
    add_error(changeset, field, "An integer is expected.")
  end
end

defp integer?(decimal) do
  decimal
  |> Decimal.round()
  |> Decimal.equal?(decimal)
end

Also Liked

Kurisu

Kurisu

Thank you @Aetherus!

it works fine. I just had to add one more clause to the integer?/1 function:

defp integer?(nil), do: false

otherwise the Decimal.round/1 function throws an exception when rendering a new empty form.

Aetherus

Aetherus

My pleasure.

I was assuming that the field should not be nil and is validated with Ecto.Changeset.validate_required before piping into this validate_integer. My fault.

al2o3cr

al2o3cr

FWIW, Ecto.Changeset.validate_change would give you the “don’t bother doing this if the value is nil” behavior as well.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement