Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate that a value is in a certain range, e.g. 1 <= val <=2

Tags:

I want to validate a number :value to be within 1 or 2

validates :value, :format => { :with => /1|2/, :message => "Select number.." } 

However, the above code is failing the validation when value == 1

Please ensure that your solution allows me to add a message for the validation.

like image 436
Yugal Jindle Avatar asked Jun 20 '11 11:06

Yugal Jindle


1 Answers

validates :value, :inclusion => {:in => [1,2]} 

See http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of

like image 141
Jakob S Avatar answered Sep 24 '22 07:09

Jakob S