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
diff --git a/.gitignore b/.gitignore index 6e869a06a8..c0117a795b 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ railties/test/500.html railties/doc/guides/html/images railties/doc/guides/html/stylesheets *.rbc +*.swp 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 default error message for validates_confirmation_of is "doesn’t match confirmation"
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.
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
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".
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.
The default error message for validates_format_of is "is invalid".
This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
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".
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.
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.
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.
The default error message for validates_numericality_of is "is not a number".
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).
The default error message for validates_presence_of is "can’t be empty".
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.
The default error message for validates_uniqueness_of is "has already been taken".
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.