# validates\_amount .. is there any??

**URL:** <https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490>\
**Category:** rubyonrails-talk\
**Created:** [September 24, 2007, 10:44am UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490 "2007-09-24T10:44:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![spongebob](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@spongebob](https://discuss.rubyonrails.org/u/spongebob)\
**Post date:** [September 24, 2007, 10:44am UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/1 "2007-09-24T10:44:21Z")

</div>

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

Thanks =]

---

<div class="post-metadata">

**Author:** ![ebrad](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@ebrad](https://discuss.rubyonrails.org/u/ebrad)\
**Post date:** [September 24, 2007, 1:30pm UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/2 "2007-09-24T13:30:16Z")

</div>

#### Taken From Agile Web Development 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:** ![Duplex](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@Duplex](https://discuss.rubyonrails.org/u/Duplex)\
**Post date:** [September 24, 2007, 1:39pm UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/3 "2007-09-24T13:39:45Z")

</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:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [September 24, 2007, 1:51pm UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/4 "2007-09-24T13:51:11Z")

</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:** ![ebrad](https://avatars.discourse-cdn.com/v4/letter/e/13edae/32.png) [@ebrad](https://discuss.rubyonrails.org/u/ebrad)\
**Post date:** [September 24, 2007, 2:42pm UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/5 "2007-09-24T14:42:42Z")

</div>

I must not have read his post carefully enough.

---

<div class="post-metadata">

**Author:** ![spongebob](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@spongebob](https://discuss.rubyonrails.org/u/spongebob)\
**Post date:** [September 25, 2007, 8:02am UTC](https://discuss.rubyonrails.org/t/validates-amount-is-there-any/18490/6 "2007-09-25T08:02:44Z")

</div>

Thanks. this helps =]
