Validates_numericality_of :not_equal_to

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?

Peace,
Phillip

Philip,

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.

-Danimal

On Apr 3, 10:03 pm, Phillip K. [email protected]

On Fri, Apr 04, 2008 at 06:03:26AM +0200, Phillip K. wrote:

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.

Peace,
Phillip
–Greg

HI Greg,

That’s kind of funny that you mention that. I was just looking through
the stock validations again, and I took a closer look at
validate_exclusion_of and thought, “Hmm. Maybe I could use that.” I had
thought about validates_numericality_of being a good match because there
is already an :equal_to attribute. It seems logical, at least to my
(possibly warped) mind, that :not_equal_to would be appropriate.

I’m leaning toward a generic validates_not_equal_to that can be used for
any string. Since I will also be using validates_numericality_of on
this particular field, I could get away with

validates_not_equal_to :my_field, :comparison => ‘0’

The upside is then it can be used for strings as well

validates_not_equal_to :email, :comparison =>
[email protected]’, :case_sensitive => false

And I’ve thought of another validation helper that I could make decent
use of:

validates_presence_of_one :field1, :field2, :field3, :field4

For those cases when one of two or more fields need to be filled in.

I suppose some could argue that these kinds of validations should be in
the model code. I guess that’s a valid position, but to each his own.

Thanks for the feedback!
Phillip

Philip,

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]

should do what you need, right?

Just some ramblings.

-Danimal

On Apr 4, 6:43 am, Phillip K. [email protected]

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.

Peace,
Phillip

At the end of the day, I did go with creating custom validations and
adding them into AR. I wrote just a little bit about it at

http://imverbose.blogspot.com/2008/04/custom-validations-in-rails-202.html

I’m not a blogger, so don’t expect too much.

Peace,
Phillip