From 3645fd15dbc397f4017d9046651fa560c45a72b1 Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Tue, 11 Nov 2008 22:34:24 -0200 Subject: Added 'Conditional Validations' to AR validations and callbacks guide --- .../html/activerecord_validations_callbacks.html | 49 +++++++++++++++++++++- .../source/activerecord_validations_callbacks.txt | 38 ++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index 29549cf591..a81c6c0c64 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -253,6 +253,15 @@ ul#navMain {
  • Conditional validation +
  • Changelog @@ -623,7 +632,7 @@ http://www.gnu.org/software/src-highlite -->

    4. Common validation options

    -

    There are some common options that all the validation helpers can use. Here they are, except for the :if option, which we'll cover right at the next topic.

    +

    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.

    4.1. The :allow_nil option

    You may use the :allow_nil option everytime you just want to trigger a validation if the value being validated is not nil. You may be asking yourself if it makes any sense to use :allow_nil and validates_presence_of together. Well, it does. Remember, validation will be skipped only for nil attributes, but empty strings are not considered nil.

    @@ -654,6 +663,44 @@ http://www.gnu.org/software/src-highlite -->

    5. Conditional validation

    +

    Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the :if and :unless options, which can take a symbol, a string or a Ruby Proc. You may use the :if option when you want to specify when the validation should happen. If you want to specify when the validation should not happen, then you may use the :unless option.

    +

    5.1. Using a symbol with the :if and :unless options

    +

    You can associated the :if and :unless options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.

    +
    +
    +
    class Order < ActiveRecord::Base
    +  validates_presence_of :card_number, :if => :paid_with_card?
    +
    +  def paid_with_card?
    +    payment_type == "card"
    +  end
    +end
    +
    +

    5.2. Using a string with the :if and :unless options

    +

    You can also use a string that will be evaluated using :eval and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.

    +
    +
    +
    class Person < ActiveRecord::Base
    +  validates_presence_of :surname, :if => "name.nil?"
    +end
    +
    +

    5.3. Using a Proc object with the :if option

    +

    Finally, it's possible to associate :if and :unless with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.

    +
    +
    +
    class Account < ActiveRecord::Base
    +  validates_confirmation_of :password, :if => Proc.new { |a| !a.password.blank? }
    +end
    +

    6. Changelog

    diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index b0838d7a9a..23978c0cb8 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -297,7 +297,7 @@ The default error message for +validates_uniqueness_of+ is "_has already been ta == Common validation options -There are some common options that all the validation helpers can use. Here they are, except for the +:if+ option, which we'll cover right at the next topic. +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. === The +:allow_nil+ option @@ -330,8 +330,44 @@ end == Conditional validation +Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a Ruby Proc. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option. +=== Using a symbol with the +:if+ and +:unless+ options +You can associated the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option. + +[source, ruby] +------------------------------------------------------------------ +class Order < ActiveRecord::Base + validates_presence_of :card_number, :if => :paid_with_card? + + def paid_with_card? + payment_type == "card" + end +end +------------------------------------------------------------------ + +=== Using a string with the +:if+ and +:unless+ options + +You can also use a string that will be evaluated using +:eval+ and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition. + +[source, ruby] +------------------------------------------------------------------ +class Person < ActiveRecord::Base + validates_presence_of :surname, :if => "name.nil?" +end +------------------------------------------------------------------ + +=== Using a Proc object with the +:if+ option + +Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners. + +[source, ruby] +------------------------------------------------------------------ +class Account < ActiveRecord::Base + validates_confirmation_of :password, :if => Proc.new { |a| !a.password.blank? } +end +------------------------------------------------------------------ == Changelog -- cgit v1.2.3