From 34a05a701bb72aca2f482c4005af9cd9ea546afe Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Wed, 25 May 2011 21:54:23 +0530 Subject: changes validates_length_of to newer syntax --- .../active_record_validations_callbacks.textile | 31 ++++++++++------------ 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile index 618d02c9d3..fbafb23417 100644 --- a/railties/guides/source/active_record_validations_callbacks.textile +++ b/railties/guides/source/active_record_validations_callbacks.textile @@ -279,10 +279,10 @@ This helper validates the length of the attributes' values. It provides a variet class Person < ActiveRecord::Base - validates_length_of :name, :minimum => 2 - validates_length_of :bio, :maximum => 500 - validates_length_of :password, :in => 6..20 - validates_length_of :registration_number, :is => 6 + validates :name, :length => { :minimum => 2 } + validates :bio, :length => { :maximum => 500 } + validates :password, :length => { :in => 6..20 } + validates :registration_number, :length => { :is => 6 } end @@ -297,8 +297,8 @@ The default error messages depend on the type of length validation being perform class Person < ActiveRecord::Base - validates_length_of :bio, :maximum => 1000, - :too_long => "%{count} characters is the maximum allowed" + validates :bio, :length => { :maximum => 1000, + :too_long => "%{count} characters is the maximum allowed" } end @@ -306,12 +306,13 @@ This helper counts characters by default, but you can split the value in a diffe class Essay < ActiveRecord::Base - validates_length_of :content, + validates :content, :length => { :minimum => 300, :maximum => 400, :tokenizer => lambda { |str| str.scan(/\w+/) }, :too_short => "must have at least %{count} words", :too_long => "must have at most %{count} words" + } end @@ -483,7 +484,7 @@ The +:allow_blank+ option is similar to the +:allow_nil+ option. This option wil class Topic < ActiveRecord::Base - validates_length_of :title, :is => 5, :allow_blank => true + validates :title, :length => { :is => 5, :allow_blank => true } end Topic.create("title" => "").valid? # => true @@ -559,7 +560,7 @@ Sometimes it is useful to have multiple validations use one condition, it can be class User < ActiveRecord::Base with_options :if => :is_admin? do |admin| - admin.validates_length_of :password, :minimum => 10 + admin.validates :password, :length => { :minimum => 10 } admin.validates :email, :presence => true end end @@ -622,8 +623,7 @@ Returns an OrderedHash with all errors. Each key is the attribute name and the v class Person < ActiveRecord::Base - validates :name, :presence => true - validates_length_of :name, :minimum => 3 + validates :name, :presence => true, :length => { :minimum => 3 } end person = Person.new @@ -642,8 +642,7 @@ h4(#working_with_validation_errors-errors-2). +errors[]+ class Person < ActiveRecord::Base - validates :name, :presence => true - validates_length_of :name, :minimum => 3 + validates :name, :presence => true, :length => { :minimum => 3 } end person = Person.new(:name => "John Doe") @@ -718,8 +717,7 @@ The +clear+ method is used when you intentionally want to clear all the messages class Person < ActiveRecord::Base - validates :name, :presence => true - validates_length_of :name, :minimum => 3 + validates :name, :presence => true, :length => { :minimum => 3 } end person = Person.new @@ -742,8 +740,7 @@ The +size+ method returns the total number of error messages for the object. class Person < ActiveRecord::Base - validates :name, :email, :presence => true - validates_length_of :name, :minimum => 3 + validates :name, :presence => true, :length => { :minimum => 3 } end person = Person.new -- cgit v1.2.3