# Validates\_amount .. is there any?

**URL:** <https://www.ruby-forum.com/t/validates-amount-is-there-any/115359>\
**Category:** Rails\
**Created:** [September 24, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359 "2007-09-24T12:45:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/1 "2007-09-24T12:45:14Z")

</div>

Is there any plugin which validates amount?? say the amount entered  
should be greater than 0.

Thanks =]

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/2 "2007-09-24T15:31:17Z")

</div>

#### Taken From Agile Web D. With Rails, 2nd Ed. p.369

validates\_length\_of

Validates that the length of the value of each of the attributes meets  
some constraint:  
at least a given length, at most a given length, between two lengths,  
or exactly a given  
length. Rather than having a single :message option, this validator  
allows separate mes-  
sages for dif ferent validation failures, although :message may still  
be used. In all options,  
the lengths may not be negative.

## EXAMPLES

validates\_length\_of :name, :maximum =\> 50  
validates\_length\_of :password, :in =\> 6…20  
validates\_length\_of :address, :minimum =\> 10,  
:message =\> “seems too short”  
end

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/3 "2007-09-24T15:40:18Z")

</div>

validates\_lenght\_of is meant for strings, it can behave unexpected  
when used on integer attributes as it uses byte comparison …

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/4 "2007-09-24T16:43:34Z")

</div>

I must not have read his post carefully enough.

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/5 "2007-09-24T15:52:24Z")

</div>

This is covered in the agile book. You need to write your own  
validation.

def validate  
errors.add :price, “must be greater than 0” unless self.price \> 0  
end

That’s a quickie off the top of my head. You may want to only do that  
check  
if the value is filled in (self.price.blank?). That’s up to you.

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 25, 2007, 10:03am UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/6 "2007-09-25T10:03:32Z")

</div>

Thanks. this helps =]

---

<div class="post-metadata">

**Author:** ![zephyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/13edae/32.png) [@zephyr](https://www.ruby-forum.com/u/zephyr)\
**Post date:** [September 24, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/validates-amount-is-there-any/115359/7 "2007-09-24T16:46:39Z")

</div>

Go look at the online Rails docs, and click on “Show source” for the  
validates\_blah functions. They are pretty straightforward.
