From afdacd0857886a021ff68e8238876daec82efefb Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Thu, 8 Jan 2009 21:00:16 -0200 Subject: Moved validates_each to the last position of it's section. Added VIM swap files to .gitignore --- .../html/activerecord_validations_callbacks.html | 46 +++++++++++----------- .../source/activerecord_validations_callbacks.txt | 30 +++++++------- 2 files changed, 38 insertions(+), 38 deletions(-) (limited to 'railties/doc/guides') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index b1f0f52736..e8889da408 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -221,8 +221,6 @@ ul#navMain {
  • The validates_confirmation_of helper
  • -
  • The validates_each helper
  • -
  • The validates_exclusion_of helper
  • The validates_format_of helper
  • @@ -237,6 +235,8 @@ ul#navMain {
  • The validates_uniqueness_of helper
  • +
  • The validates_each helper
  • +
  • @@ -504,20 +504,7 @@ http://www.gnu.org/software/src-highlite --> validates_presence_of :email_confirmation end

    The default error message for validates_confirmation_of is "doesn’t match confirmation"

    -

    3.4. The validates_each helper

    -

    This helper validates attributes against a block. It doesn’t have a predefined validation function. You should create one using a block, and every attribute passed to validates_each will be tested against it. In the following example, we don’t want names and surnames to begin with lower case.

    -
    -
    -
    class Person < ActiveRecord::Base
    -  validates_each :name, :surname do |model, attr, value|
    -    model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
    -  end
    -end
    -

    The block receives the model, the attribute’s name and the attribute’s value. If your validation fails, you can add an error message to the model, therefore making it invalid.

    -

    3.5. The validates_exclusion_of helper

    +

    3.4. The validates_exclusion_of helper

    This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.

    end

    The validates_exclusion_of helper has an option :in that receives the set of values that will not be accepted for the validated attributes. The :in option has an alias called :within that you can use for the same purpose, if you’d like to. In the previous example we used the :message option to show how we can personalize it with the current attribute’s value, through the %s format mask.

    The default error message for validates_exclusion_of is "is not included in the list".

    -

    3.6. The validates_format_of helper

    +

    3.5. The validates_format_of helper

    This helper validates the attributes’s values by testing if they match a given pattern. This pattern must be specified using a Ruby regular expression, which must be passed through the :with option.

    :message => "Only letters allowed" end

    The default error message for validates_format_of is "is invalid".

    -

    3.7. The validates_inclusion_of helper

    +

    3.6. The validates_inclusion_of helper

    This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.

    end

    The validates_inclusion_of helper has an option :in that receives the set of values that will be accepted. The :in option has an alias called :within that you can use for the same purpose, if you’d like to. In the previous example we used the :message option to show how we can personalize it with the current attribute’s value, through the %s format mask.

    The default error message for validates_inclusion_of is "is not included in the list".

    -

    3.8. The validates_length_of helper

    +

    3.7. The validates_length_of helper

    This helper validates the length of your attribute’s value. It can receive a variety of different options, so you can specify length contraints in different ways.

    validates_length_of :bio, :too_long => "you're writing too much. %d characters is the maximum allowed." end

    This helper has an alias called validates_size_of, it’s the same helper with a different name. You can use it if you’d like to.

    -

    3.9. The validates_numericality_of helper

    +

    3.8. The validates_numericality_of helper

    This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the :integer_only option set to true, you can specify that only integral numbers are allowed.

    If you use :integer_only set to true, then it will use the /\A[+\-]?\d+\Z/+ regular expression to validate the attribute’s value. Otherwise, it will try to convert the value using +Kernel.Float.

    @@ -614,7 +601,7 @@ http://www.gnu.org/software/src-highlite --> validates_numericality_of :games_played, :integer_only => true end

    The default error message for validates_numericality_of is "is not a number".

    -

    3.10. The validates_presence_of helper

    +

    3.9. The validates_presence_of helper

    This helper validates that the attributes are not empty. It uses the blank? method to check if the value is either nil or an empty string (if the string has only spaces, it will still be considered empty).

    Note -If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true +If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in ⇒ [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # ⇒ true

    The default error message for validates_presence_of is "can’t be empty".

    -

    3.11. The validates_uniqueness_of helper

    +

    3.10. The validates_uniqueness_of helper

    This helper validates that the attribute’s value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you wish were unique. To avoid that, you must create an unique index in your database.

    validates_uniqueness_of :name, :case_sensitive => false end

    The default error message for validates_uniqueness_of is "has already been taken".

    +

    3.11. The validates_each helper

    +

    This helper validates attributes against a block. It doesn’t have a predefined validation function. You should create one using a block, and every attribute passed to validates_each will be tested against it. In the following example, we don’t want names and surnames to begin with lower case.

    +
    +
    +
    class Person < ActiveRecord::Base
    +  validates_each :name, :surname do |model, attr, value|
    +    model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
    +  end
    +end
    +

    The block receives the model, the attribute’s name and the attribute’s value. If your validation fails, you can add an error message to the model, therefore making it invalid.

    4. Common validation options

    diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index ff2acdf8d5..9865aeebb0 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -133,21 +133,6 @@ end The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_" -=== The +validates_each+ helper - -This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case. - -[source, ruby] ------------------------------------------------------------------- -class Person < ActiveRecord::Base - validates_each :name, :surname do |model, attr, value| - model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/ - end -end ------------------------------------------------------------------- - -The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid. - === The +validates_exclusion_of+ helper This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object. @@ -301,6 +286,21 @@ end The default error message for +validates_uniqueness_of+ is "_has already been taken_". +=== The +validates_each+ helper + +This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case. + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base + validates_each :name, :surname do |model, attr, value| + model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/ + end +end +------------------------------------------------------------------ + +The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid. + == Common validation options There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which we'll cover right at the next topic. -- cgit v1.2.3