Greater than zero validation doesn't work on Zero

I’m trying to perfom validation on a liveview field but can’t wrap my mind around what the problem is

Ecto.Changeset.validate_number(:number_field, greater_than: 0)

the changeset does not return any errors when the input value is 0. It does return an error when the value is -1 to -n. I have alternatively tried using less than, here is a snippet of the same

Ecto.Changeset.validate_number(:number_field, less_than: 0)

Is there an alternative way to validate the number field to make sure it is never going to be zero.

Are you sure the value is changed to 0 – as opposed to already being zero/defaulting to zero or not being set?

For not being set you’d need to add validate_required as well and for the already 0 or default to 0 case you need to either use force_change on cast or make sure the value is not already 0 before adding the changes.

1 Like