0
@@ -479,8 +479,9 @@ module ActiveRecord
0
# validates_length_of :fax, :in => 7..32, :allow_nil => true
0
# validates_length_of :phone, :in => 7..32, :allow_blank => true
0
# validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
0
- # validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
0
- # validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
0
+ # validates_length_of :fav_bra_size, :minimum => 1, :too_short => "please enter at least %d character"
0
+ # validates_length_of :smurf_leader, :is => 4, :message => "papa is spelled with %d characters... don't play me."
0
+ # validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least %d words."), :tokenizer => lambda {|str| str.scan(/\w+/) }
0
# Configuration options:
0
@@ -491,7 +492,6 @@ module ActiveRecord
0
# * <tt>:in</tt> - A synonym(or alias) for <tt>:within</tt>.
0
# * <tt>:allow_nil</tt> - Attribute may be +nil+; skip validation.
0
# * <tt>:allow_blank</tt> - Attribute may be blank; skip validation.
0
# * <tt>:too_long</tt> - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)").
0
# * <tt>:too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)").
0
# * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt> method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)").
0
@@ -503,12 +503,16 @@ module ActiveRecord
0
# * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
0
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
0
# method, proc or string should return or evaluate to a true or false value.
0
+ # * <tt>:tokenizer</tt> - Specifies how to split up the attribute string. (e.g. <tt>:tokenizer => lambda {|str| str.scan(/\w+/)}</tt> to
0
+ # count words as in above example.)
0
+ # Defaults to <tt>lambda{ |value| value.split(//) }</tt> which counts individual characters.
0
def validates_length_of(*attrs)
0
# Merge given options with defaults.
0
:too_long => ActiveRecord::Errors.default_error_messages[:too_long],
0
:too_short => ActiveRecord::Errors.default_error_messages[:too_short],
0
- :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length]
0
+ :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length],
0
+ :tokenizer => lambda {|value| value.split(//)}
0
}.merge(DEFAULT_VALIDATION_OPTIONS)
0
options.update(attrs.extract_options!.symbolize_keys)
0
@@ -535,7 +539,7 @@ module ActiveRecord
0
too_long = options[:too_long] % option_value.end
0
validates_each(attrs, options) do |record, attr, value|
0
- value =
value.split(//) if value.kind_of?(String)
0
+ value =
options[:tokenizer].call(value) if value.kind_of?(String)
0
if value.nil? or value.size < option_value.begin
0
record.errors.add(attr, too_short)
0
elsif value.size > option_value.end
0
@@ -552,7 +556,7 @@ module ActiveRecord
0
message = (options[:message] || options[message_options[option]]) % option_value
0
validates_each(attrs, options) do |record, attr, value|
0
- value =
value.split(//) if value.kind_of?(String)
0
+ value =
options[:tokenizer].call(value) if value.kind_of?(String)
0
record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value]
Comments
No one has commented yet.