Does this a design gotcha? Maybe more likely is a bug.
I have read the code, and found that only :is, :maximum, :mininum will
accept the :message option.
Below is the related code.
442: def validates_length_of(*attrs)
443: # Merge given options with defaults.
444: options = {
445: :too_long =>
ActiveRecord::Errors.default_error_messages[:too_long],
446: :too_short =>
ActiveRecord::Errors.default_error_messages[:too_short],
447: :wrong_length =>
ActiveRecord::Errors.default_error_messages[:wrong_length]
448: }.merge(DEFAULT_VALIDATION_OPTIONS)
449: options.update(attrs.pop.symbolize_keys) if
attrs.last.is_a?(Hash)
450:
451: # Ensure that one and only one range option is specified.
452: range_options = ALL_RANGE_OPTIONS & options.keys
453: case range_options.size
454: when 0
455: raise ArgumentError, ‘Range unspecified. Specify
the :within, :maximum, :minimum, or :is option.’
456: when 1
457: # Valid number of options; do nothing.
458: else
459: raise ArgumentError, ‘Too many range options
specified. Choose only one.’
460: end
461:
462: # Get range option and value.
463: option = range_options.first
464: option_value = options[range_options.first]
465:
466: case option
467: when :within, :in
468: raise ArgumentError, “:#{option} must be a Range”
unless option_value.is_a?(Range)
469:
470: too_short = options[:too_short] % option_value.begin
471: too_long = options[:too_long] % option_value.end
472:
473: validates_each(attrs, options) do |record, attr,
value|
474: if value.nil? or value.split(//).size <
option_value.begin
475: record.errors.add(attr, too_short)
476: elsif value.split(//).size > option_value.end
477: record.errors.add(attr, too_long)
478: end
479: end
480: when :is, :minimum, :maximum
481: raise ArgumentError, “:#{option} must be a
nonnegative Integer” unless option_value.is_a?(Integer) and
option_value >= 0
482:
483: # Declare different validations per option.
484: validity_checks = { :is => “==”, :minimum =>
“>=”, :maximum => “<=” }
485: message_options = { :is => :wrong_length, :minimum
=> :too_short, :maximum => :too_long }
486:
487: message = (options[:message] ||
options[message_options[option]]) % option_value
488:
489: validates_each(attrs, options) do |record, attr,
value|
490: if value.kind_of?(String)
491: record.errors.add(attr, message) unless !
value.nil? and value.split(//).size.method(validity_checks[option])
[option_value]
492: else
493: record.errors.add(attr, message) unless !
value.nil? and value.size.method(validity_checks[option])
[option_value]
494: end
495: end
496: end
497: end