instance method
validates_length_of
Ruby on Rails 2.3.18
Since v2.2.3Signature
validates_length_of(*attrs)
Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time:
class Person < ActiveRecord::Base
validates_length_of :first_name, :maximum => 30
validates_length_of :last_name, :maximum => 30, :message => "less than %{count} if you don't mind"
validates_length_of :fax, :in => 7..32, :allow_nil => true
validates_length_of :phone, :in => 7..32, :allow_blank => true
validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
validates_length_of :zip_code, :minimum => 5, :too_short => "please enter at least %{count} characters"
validates_length_of :smurf_leader, :is => 4, :message => "papa is spelled with %{count} characters... don't play me"
validates_length_of :essay, :minimum => 100, :too_short => "Your essay must be at least %{count} words"), :tokenizer => lambda {|str| str.scan(/\w+/) }
end
Configuration options:
-
:minimum- The minimum size of the attribute. -
:maximum- The maximum size of the attribute. -
:is- The exact size of the attribute. -
:within- A range specifying the minimum and maximum size of the attribute. -
:in- A synonym(or alias) for:within. -
:allow_nil- Attribute may benil; skip validation. -
:allow_blank- Attribute may be blank; skip validation. -
:too_long- The error message if the attribute goes over the maximum (default is: “is too long (maximum is %{count} characters)”). -
:too_short- The error message if the attribute goes under the minimum (default is: “is too short (min is %{count} characters)”). -
:wrong_length- The error message if using the:ismethod and the attribute is the wrong size (default is: “is the wrong length (should be %{count} characters)”). -
:message- The error message to use for a:minimum,:maximum, or:isviolation. An alias of the appropriatetoo_long/too_short/wrong_lengthmessage. -
:on- Specifies when this validation is active (default is:save, other options:create,:update). -
:if- Specifies a method, proc or string to call to determine if the validation should occur (e.g.:if => :allow_validation, or:if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value. -
:unless- Specifies a method, proc or string to call to determine if the validation should not occur (e.g.:unless => :skip_validation, or:unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value. -
:tokenizer- Specifies how to split up the attribute string. (e.g.:tokenizer => lambda {|str| str.scan(/\w+/)}to count words as in above example.) Defaults tolambda{ |value| value.split(//) }which counts individual characters.
Parameters
-
attrsrest
Source
# File activerecord/lib/active_record/validations.rb, line 637
def validates_length_of(*attrs)
# Merge given options with defaults.
options = {
:tokenizer => lambda {|value| value.split(//)}
}.merge(DEFAULT_VALIDATION_OPTIONS)
options.update(attrs.extract_options!.symbolize_keys)
# Ensure that one and only one range option is specified.
range_options = ALL_RANGE_OPTIONS & options.keys
case range_options.size
when 0
raise ArgumentError, 'Range unspecified. Specify the :within, :maximum, :minimum, or :is option.'
when 1
# Valid number of options; do nothing.
else
raise ArgumentError, 'Too many range options specified. Choose only one.'
end
# Get range option and value.
option = range_options.first
option_value = options[range_options.first]
key = {:is => :wrong_length, :minimum => :too_short, :maximum => :too_long}[option]
custom_message = options[:message] || options[key]
case option
when :within, :in
raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range)
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
if value.nil? or value.size < option_value.begin
record.errors.add(attr, :too_short, :default => custom_message || options[:too_short], :count => option_value.begin)
elsif value.size > option_value.end
record.errors.add(attr, :too_long, :default => custom_message || options[:too_long], :count => option_value.end)
end
end
when :is, :minimum, :maximum
raise ArgumentError, ":#{option} must be a nonnegative Integer" unless option_value.is_a?(Integer) and option_value >= 0
# Declare different validations per option.
validity_checks = { :is => "==", :minimum => ">=", :maximum => "<=" }
validates_each(attrs, options) do |record, attr, value|
value = options[:tokenizer].call(value) if value.kind_of?(String)
unless !value.nil? and value.size.method(validity_checks[option])[option_value]
record.errors.add(attr, key, :default => custom_message, :count => option_value)
end
end
end
end
Defined in activerecord/lib/active_record/validations.rb line 637
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Validations::ClassMethods