# Why validates\_length\_of + :within just discard :message?

**URL:** <https://www.ruby-forum.com/t/why-validates-length-of-within-just-discard-message/105870>\
**Category:** Rails\
**Created:** [June 4, 2007, 6:17am UTC](https://www.ruby-forum.com/t/why-validates-length-of-within-just-discard-message/105870 "2007-06-04T06:17:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sishen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@sishen](https://www.ruby-forum.com/u/sishen)\
**Post date:** [June 4, 2007, 6:17am UTC](https://www.ruby-forum.com/t/why-validates-length-of-within-just-discard-message/105870/1 "2007-06-04T06:17:35Z")

</div>

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
