method
validates_length_of
v2.0.3 -
Show latest stable
-
1 note -
Class: ActiveRecord::Validations::ClassMethods
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (0)
- 2.0.3 (5)
- 2.1.0 (2)
- 2.2.1 (6)
- 2.3.8 (0)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1 (-35)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
validates_length_of(*attrs)
public
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 %d 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 :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character" validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me." 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 be nil; 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 %d characters)")
- too_short - The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)")
- wrong_length - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
- message - The error message to use for a :minimum, :maximum, or :is violation. An alias of the appropriate too_long/too_short/wrong_length message
- 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.
Register or
log in
to add new notes.
sjmadsen -
March 13, 2010
3 thanks
Can be used with has_many associations
You can also use this to validate that a has_many association has a specified number of records on the other end:
has_many :members validates_length_of :members, :minimum => 1