# Validation help

**URL:** <https://www.ruby-forum.com/t/validation-help/64035>\
**Category:** Rails\
**Created:** [June 21, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/validation-help/64035 "2006-06-21T15:14:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 21, 2006, 3:14pm UTC](https://www.ruby-forum.com/t/validation-help/64035/1 "2006-06-21T15:14:07Z")

</div>

I would like to validate a number within a certain range.  
To first test that a entry is a valid number i use  
validates\_numericality\_of :my\_number, :message =\>‘sorry, not a number’

to test if the number is within a certain range i use  
validates\_inclusion of :my\_number, :in =\> min…max, message =\> ‘outside  
range!’

If the numericality test fails, I don’t want to do the second range  
test, otherwise both error messages will be displayed. At the moment,  
both tests with be performed regardless. Any ideas on how to do this?

Thanks.

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 21, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/validation-help/64035/2 "2006-06-21T17:46:11Z")

</div>

What would happen if you just take off the validates\_numericality\_of,  
and left only validates\_inclusion?

I know about two ways to solve your problem:

a) you can define a validate method in your method, do the validations  
by yourself, and if you found any error, you can add them to the array  
errors, example:

def validate  
if (price == nil)  
errors.add “There is not price!!!”  
else if (price \< 0)  
errors.add “The price is negative!!!”  
end  
end

b) You can check what errors to display, in the view, by inspecting  
modelobject.errors

Rodrigo D.

Iplan Networks Datos Personales  
removed\_email\_address@domain.invalid removed\_email\_address@domain.invalid  
www.iplan.com.ar www.rorra.com.ar  
5031-6303 15-5695-6027

-----Mensaje original-----  
De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] En nombre de montyman  
Enviado el: Miércoles, 21 de Junio de 2006 10:14 a.m.  
Para: removed\_email\_address@domain.invalid  
Asunto: [Rails] validation help

I would like to validate a number within a certain range.  
To first test that a entry is a valid number i use  
validates\_numericality\_of :my\_number, :message =\>‘sorry, not a number’

to test if the number is within a certain range i use  
validates\_inclusion of :my\_number, :in =\> min…max, message =\> ‘outside  
range!’

If the numericality test fails, I don’t want to do the second range  
test, otherwise both error messages will be displayed. At the moment,  
both tests with be performed regardless. Any ideas on how to do this?

Thanks.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [June 22, 2006, 1:34am UTC](https://www.ruby-forum.com/t/validation-help/64035/3 "2006-06-22T01:34:46Z")

</div>

Hey Monty, if you goal is to validate that a field is within a certain  
range, then I would recommend using ‘validates\_inclusion’.

-Conrad

---

<div class="post-metadata">

**Author:** ![montyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/76d3ee/32.png) [@montyman](https://www.ruby-forum.com/u/montyman)\
**Post date:** [January 16, 2007, 5:46am UTC](https://www.ruby-forum.com/t/validation-help/64035/4 "2007-01-16T05:46:35Z")

</div>

thank you conrad for posting that tip!

Conrad T. wrote:

> Hey Monty, if you goal is to validate that a field is within a certain  
> range, then I would recommend using ‘validates\_inclusion’.
> 
> -Conrad
