# Validates\_length\_of with char field

**URL:** <https://www.ruby-forum.com/t/validates-length-of-with-char-field/65279>\
**Category:** Rails\
**Created:** [August 17, 2006, 8:49am UTC](https://www.ruby-forum.com/t/validates-length-of-with-char-field/65279 "2006-08-17T08:49:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [August 17, 2006, 8:49am UTC](https://www.ruby-forum.com/t/validates-length-of-with-char-field/65279/1 "2006-08-17T08:49:18Z")

</div>

I am new to ruby/rails and am having a problem with validates\_length\_of.  
It seems to work fine on a varchar field, but when I try it on a char  
field the extra characters are silently truncated and the message  
appears that the record was successfully updated.

How can I get my app to produce my error message instead? I’ve tried  
various variations, but here’s one of my tries:

validates\_length\_of :State, :minimum =\> 2, :maximum =\> 2,  
:message =\> “Use the two-letter state abbreviation”

I know I can use a drop-down list instead (that’s my next exercise) but  
I’d like to know how to do this for other char fields as well. Or is the  
char issue a red herring and something else is wrong with my code?

Thanks,  
Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [August 17, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/validates-length-of-with-char-field/65279/2 "2006-08-17T20:36:06Z")

</div>

don’t know if using :minimum and :maximum would cause any problems,  
but you could just use:

validates\_length\_of :state, :is =\> 2, :message =\> “Use the two-letter  
state abbreviation”

or even

validates\_format\_of :state, :with =\> /^[a-zA-Z]{2}$/

that way you know you are getting a 2 character field that only  
contains A-Z and or a-z characters.

Chris

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [August 18, 2006, 7:03am UTC](https://www.ruby-forum.com/t/validates-length-of-with-char-field/65279/3 "2006-08-18T07:03:45Z")

</div>

Thanks, Chris. They both worked!

My problem was that I was adding the validates\_length\_of :State line to  
the wrong version of my app. But for other newbies - Rails still didn’t  
like the :minimum, :maximum combo and replied “too many arguments”.

Shauna
