This project is archived and is in readonly mode.

#5283 ✓resolved
Jakub Suder

wrong message used in validates_length_of

Reported by Jakub Suder | August 3rd, 2010 @ 10:51 AM | in 3.0.2

If both :too_short and :too_long messages are defined for validates_length_of, it uses the :too_short message even when the value is too long.

With such model:

class User < ActiveRecord::Base
  validates_length_of :username, :minimum => 3, :maximum => 10, :too_short => 'too short', :too_long => 'too long'

This is what you get:

u = :username => 'js'
u.errors  # => {:username=>["too short"]} 

u = :username => 'jakub_suder'
u.errors  # => {:username=>["too short"]}

The reason is probably this line in validate_each method in length.rb:

options[:message] ||= default_message if default_message

Once it passes through the too_short validation (even though it doesn't add the error in the end, because valid_value returns true), options[:message] is set and it's not changed again when it comes to too_long validation.

It was probably broken in this commit: (there might be other validations that have been broken this way, though this one is unique because it has two different messages and most validations have just one).

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>


Referenced by