From 0c3752390cf9b4f64169e9eb5ad860aec691edeb Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Fri, 7 Nov 2008 20:51:27 -0200 Subject: Added some more text about how validations work, included validates_acceptance_of --- .../html/activerecord_validations_callbacks.html | 115 ++++++++++++++++++++- .../source/activerecord_validations_callbacks.txt | 84 ++++++++++++++- 2 files changed, 192 insertions(+), 7 deletions(-) (limited to 'railties') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index f6c8fc668e..a645f175fd 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -202,7 +202,47 @@ ul#navMain { Motivations to validate your Active Record objects
  • - When does validation happens? + How it works + +
  • +
  • + The declarative validation helpers + +
  • +
  • + Common validation options
  • Credits @@ -275,14 +315,79 @@ Using validation directly into your Active Record classes ensures that only vali
  • -

    2. When does validation happens?

    +

    2. How it works

    +
    +

    2.1. When does validation happens?

    +

    There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the new method, that object does not belong to the database yet. Once you call save upon that object it'll be recorded to it's table. Active Record uses the new_record? instance method to discover if an object is already in the database or not. Consider the following simple and very creative Active Record class:

    +
    +
    +
    class Person < ActiveRecord::Base
    +end
    +
    +

    We can see how it works by looking at the following script/console output:

    +
    +
    +
    >> p = Person.new(:name => "John Doe", :birthdate => Date.parse("09/03/1979"))
    +=> #<Person id: nil, name: "John Doe", birthdate: "1979-09-03", created_at: nil, updated_at: nil>
    +>> p.new_record?
    +=> true
    +>> p.save
    +=> true
    +>> p.new_record?
    +=> false
    +
    +

    Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either save, update_attribute or update_attributes) will result in a SQL update operation. Active Record will use this facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.

    +

    2.2. The meaning of valid

    +

    For verifying if an object is valid, Active Record uses the valid? method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the errors instance method. The proccess is really simple: If the errors method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the errors collection.

    +
    +

    3. The declarative validation helpers

    +
    +

    Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validations rules that are commonly used in most of the applications that you'll write, so you don't need to recreate it everytime, avoiding code duplication, keeping everything organized and boosting your productivity. Everytime a validation fails, an error message is added to the object's errors collection, this message being associated with the field being validated.

    +

    All these helpers accept the :on and :message options, which define when the validation should be applied and what message should be added to the errors collection when it fails, respectively. The :on option takes one the values :save (it's the default), :create or :update. There is a default error message for each one of the validation helpers. These messages are used when the :message option isn't used. Let's take a look at each one of the available helpers, listed in alphabetic order.

    +

    3.1. The validates_acceptance_of helper

    +

    Validates that a checkbox has been checked for agreement purposes. It's normally used when the user needs to agree with your application's terms of service, confirm reading some clauses or any similar concept. This validation is very specific to web applications and actually this acceptance does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).

    +
    +
    +
    class Person < ActiveRecord::Base
    +  validates_acceptance_of :terms_of_service
    +end
    +
    +

    The default error message for validates_acceptance_of is "must be accepted"

    +

    validates_acceptance_of can receive an :accept option, which determines the value that will be considered acceptance. It defaults to "1", but you can change it.

    +
    +
    +
    class Person < ActiveRecord::Base
    +  validates_acceptance_of :terms_of_service, :accept => 'yes'
    +end
    +
    +

    3.2. The validates_associated helper

    +

    3.3. The validates_confirmation_of helper

    +

    3.4. The validates_each helper

    +

    3.5. The validates_exclusion_of helper

    +

    3.6. The validates_format_of helper

    +

    3.7. The validates_inclusion_of helper

    +

    3.8. The validates_length_of helper

    +

    3.9. The validates_numericallity_of helper

    +

    3.10. The validates_presence_of helper

    +

    3.11. The validates_size_of helper

    +

    3.12. The validates_uniqueness_of+ helper

    +
    +

    4. Common validation options

    -

    There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the new method, that object does not belong to the database yet. Once you call save upon that object it'll be recorded to it's table. Active Record uses the new_record? instance method to discover if an object is already in the database or not. Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either save, update_attribute or update_attributes) will result in a SQL update operation. Active Record will use this facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.

    -

    3. Credits

    +

    5. Credits

    -

    4. Changelog

    +

    6. Changelog

    diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index b1fdfced05..d0d9d265aa 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -22,9 +22,89 @@ There are several ways to validate the data that goes to the database, like usin * Implementing validations only at the client side can be problematic, specially with web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data. * Using validation directly into your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the hability to easily create validations, using several built-in helpers while still allowing you to create your own validation methods. -== When does validation happens? +== How it works -There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it'll be recorded to it's table. Active Record uses the +new_record?+ instance method to discover if an object is already in the database or not. Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either +save+, +update_attribute+ or +update_attributes+) will result in a SQL update operation. Active Record will use this facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one. +=== When does validation happens? + +There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it'll be recorded to it's table. Active Record uses the +new_record?+ instance method to discover if an object is already in the database or not. Consider the following simple and very creative Active Record class: + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base +end +------------------------------------------------------------------ + +We can see how it works by looking at the following script/console output: + +------------------------------------------------------------------ +>> p = Person.new(:name => "John Doe", :birthdate => Date.parse("09/03/1979")) +=> # +>> p.new_record? +=> true +>> p.save +=> true +>> p.new_record? +=> false +------------------------------------------------------------------ + +Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either +save+, +update_attribute+ or +update_attributes+) will result in a SQL update operation. Active Record will use this facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one. + +=== The meaning of 'valid' + +For verifying if an object is valid, Active Record uses the +valid?+ method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the +errors+ instance method. The proccess is really simple: If the +errors+ method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the +errors+ collection. + +== The declarative validation helpers + +Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validations rules that are commonly used in most of the applications that you'll write, so you don't need to recreate it everytime, avoiding code duplication, keeping everything organized and boosting your productivity. Everytime a validation fails, an error message is added to the object's +errors+ collection, this message being associated with the field being validated. + +All these helpers accept the +:on+ and +:message+ options, which define when the validation should be applied and what message should be added to the +errors+ collection when it fails, respectively. The +:on+ option takes one the values +:save+ (it's the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't used. Let's take a look at each one of the available helpers, listed in alphabetic order. + +=== The +validates_acceptance_of+ helper + +Validates that a checkbox has been checked for agreement purposes. It's normally used when the user needs to agree with your application's terms of service, confirm reading some clauses or any similar concept. This validation is very specific to web applications and actually this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute). + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base + validates_acceptance_of :terms_of_service +end +------------------------------------------------------------------ + +The default error message for +validates_acceptance_of+ is "_must be accepted_" + ++validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change it. + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base + validates_acceptance_of :terms_of_service, :accept => 'yes' +end +------------------------------------------------------------------ + + +=== The +validates_associated+ helper + +=== The +validates_confirmation_of+ helper + +=== The +validates_each+ helper + +=== The +validates_exclusion_of+ helper + +=== The +validates_format_of+ helper + +=== The +validates_inclusion_of+ helper + +=== The +validates_length_of+ helper + +=== The +validates_numericallity_of+ helper + +=== The +validates_presence_of+ helper + +=== The +validates_size_of+ helper + +=== The validates_uniqueness_of+ helper + +== Common validation options -- cgit v1.2.3