small medium large xlarge



Dave says:

I added the following line to the file product.rb in the app/models directory

validates_length_of :title, :minimum => 10

To find the syntax, I looked up the method on You may add an alert message:

validates_length_of :title, :minimum => 10, :message => "at least 10 characters" 

Ana says:

You can also use an already implemented error message by using :too_short instead of message. So you can also write:

validates_length_of :title, :minimum => 10, :too_short

Florent says:

validates_length_of :title, :minimum => 10, :too_short
did not work for me (with Rails 2.3.2), I got in my browser the error /depot/app/models/product.rb:5: syntax error, unexpected '\n', expecting tASSOC I had to specify a message for :too_short:

validates_length_of :title, :minimum => 10, :too_short => "must have at least %d characters" 

Max says:

I’m not sure about the old version of Rails, but you can completely bypass the ”:too_short” part, leaving:

validates_length_of :title, :minimum => 10

Claudio says:

In Rails 3 you can now use:

validates :title, :length => { :minimum => 10 }

Dominik says:

At first I tried:

validates :title, :length => {:greater_than_or_equal_to => 10}
but it didn’t work. This one works:
validates :title, :length => {:minimum => 10}

André says:

Added validation test:

test "product title must be at least ten characters long" do
    product = products(:ruby)
    assert product.valid?, "product title shouldn't be invalid" 

    product.title = product.title.first(9)
    assert product.invalid?, "product title shouldn't be valid" 
Is this the right approach? Thanks.
Page History
  • V29: Eric Ricketts [7 months ago]
  • V28: Eric Ricketts [7 months ago]
  • V27: Stevie Braga [over 1 year ago]
  • V26: Stevie Braga [over 1 year ago]
  • V25: Stevie Braga [over 1 year ago]
  • V24: Stevie Braga [over 1 year ago]
  • V23: Stevie Braga [over 1 year ago]
  • V24: Adrian Swarzyński [over 3 years ago]
  • V23: Adrian Swarzyński [over 3 years ago]
  • V22: Ingo Klemm [over 4 years ago]