I have a situation in which I want to validate a number is not zero.
Both positive and negative values are acceptable, just not zero.
validates_numericality_of has an :equal_to attribute, but I don't see a
:not_equal_to. I have not yet been able to find anyone who has
discussed this. I did find information about custom validators, so I am
now using a custom validator to check the field and add an appropriate
error message.
Is there an easier way, ideally without using validates_format_of? It
seems like it would be a relatively common problem. How do others solve
it?
One thought: roll your own validation helper. I.e. take the source
code to validates_numericality_of and create your own, maybe something
like: validates_nonzeroness_of
It would be pretty simply as it's probably what you have in your
custom validator. But then you could define it within the context of a
helper or a lib and then use it the same as the other validates_
calls.
It's possible you could do it with validates_format_of but that might
not be the best. You need to do a two-check validation. One, that the
field is an integer and two, that it is not equal to zero.
I have a situation in which I want to validate a number is not zero.
Both positive and negative values are acceptable, just not zero.
validates_numericality_of has an :equal_to attribute, but I don't see a
:not_equal_to. I have not yet been able to find anyone who has
discussed this. I did find information about custom validators, so I am
now using a custom validator to check the field and add an appropriate
error message.
Is there an easier way, ideally without using validates_format_of? It
seems like it would be a relatively common problem. How do others solve
it?
Don't expect any single validation to necessarily take care of all your
validation needs for a particular field. I'd use validates_exclusion_of for
this, in addition to validates_numericality_of.
Thanks for the thought, Danimal. I have been thinking about doing
something like that, but wanted to see if someone else has already done
it, and if so, how they did. If I can't find anything suitable, I'll
probably create something like a validates_not_equal_to so it can be a
bit more versatile. I found something yesterday (though now I can't
remember where it is) that someone did that and added it in.
If you look at all the different validations and all their different
options, there is some overlap. I think it's good overlap... i.e. when
a particular validation makes sense to have X,Y and Z as options, it
shouldn't matter if another validation already has Y. Others may
disagree, but I like the idea that, for example,
validates_numericality_of has the right options for all the
numericality validations I'd need.
Of course, it could get too convoluted and the argument for using
multiple validations for a single attribute has value as well.
So I can see both sides of it, although I lean more toward the
"overlap is ok" side. (Oh no! The DRY police are banging on my door!)
And based on that, I'd say: add a :not_equal_to to
validates_numericality_of and then maybe also add a generic
validates_no_equal_to.
But only if you are going to release it. If you just need it to solve
your particular issue, then adding a :not_equal_to is the best and
quickest solution, IMO. You'll already have the numeric validation and
you can get the not equal to 0 as a parameter on the validation
instead of a separate validation call.
If you'd rather not write any custom code, then I suspect that:
validates_numericality_of :number
validates_exclusion_of :number, :in => [0]