Rails Validation: Limit input to specific values
validates_inclusion_of should work. For example:
validates_inclusion_of :attr, :in => [-5, -2, 2, 5], :allow_nil => true
Rails 3+
The modern way to do this is the following:
validates :field, inclusion: { in: [ -5, -2, 2, 5 ], allow_blank: true, message: "not an allowable number." }
You want to use validates_inclusion_of with the :in
and :allow_nil
options.
validates_inclusion_of :field, :in => %w(-5 -2 2 5), :allow_nil => true
You'll probably also want to use in conjunction with validates_numericality_of