From fdd1ef5ca26de29eee3e5246e609c883ea3d9319 Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Wed, 12 Nov 2008 21:40:29 -0200 Subject: Added 'custom validations' to AR validations and callbacks guide --- .../html/activerecord_validations_callbacks.html | 38 +++++++++++++++++++++- .../source/activerecord_validations_callbacks.txt | 30 +++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) (limited to 'railties/doc') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index 209722e9e0..45eec6ffa1 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -264,6 +264,9 @@ ul#navMain {
  • + Writing your own validation methods +
  • +
  • Changelog
  • @@ -702,7 +705,40 @@ http://www.gnu.org/software/src-highlite --> end -

    6. Changelog

    +

    6. Writing your own validation methods

    +
    +

    When the built-in validation helpers are not enough for your needs, you can write your own validation methods, by implementing one or more of the validate, validate_on_create or validate_on_update methods. As the names of the methods states, the right method to implement depends on when you want the validations to be ran. The meaning of valid is still the same: to make an object invalid you just need to add a message to it's errors collection.

    +
    +
    +
    class Invoice < ActiveRecord::Base
    +  def validate_on_create
    +    errors.add(:expiration_date, "can't be in the past") if !expiration_date.blank? and expiration_date < Date.today
    +  end
    +end
    +
    +

    If your validation rules are too complicated and you want to break it in small methods, you can implement all of them and call one of validate, validate_on_create or validate_on_update methods, passing it the symbols for the methods' names.

    +
    +
    +
    class Invoice < ActiveRecord::Base
    +  validate :expiration_date_cannot_be_in_the_past, :discount_cannot_be_be_more_than_total_value
    +
    +  def expiration_date_cannot_be_in_the_past
    +    errors.add(:expiration_date, "can't be in the past") if !expiration_date.blank? and expiration_date < Date.today
    +  end
    +
    +  def discount_cannot_be_greater_than_total_value
    +    errors.add(:discount, "can't be greater than total value") unless discount <= total_value
    +  end
    +end
    +
    +
    +

    7. Changelog

    diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index 29be2182ce..3da4f0361b 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -369,6 +369,36 @@ class Account < ActiveRecord::Base end ------------------------------------------------------------------ +== Writing your own validation methods + +When the built-in validation helpers are not enough for your needs, you can write your own validation methods, by implementing one or more of the +validate+, +validate_on_create+ or +validate_on_update+ methods. As the names of the methods states, the right method to implement depends on when you want the validations to be ran. The meaning of valid is still the same: to make an object invalid you just need to add a message to it's +errors+ collection. + +[source, ruby] +------------------------------------------------------------------ +class Invoice < ActiveRecord::Base + def validate_on_create + errors.add(:expiration_date, "can't be in the past") if !expiration_date.blank? and expiration_date < Date.today + end +end +------------------------------------------------------------------ + +If your validation rules are too complicated and you want to break it in small methods, you can implement all of them and call one of +validate+, +validate_on_create+ or +validate_on_update+ methods, passing it the symbols for the methods' names. + +[source, ruby] +------------------------------------------------------------------ +class Invoice < ActiveRecord::Base + validate :expiration_date_cannot_be_in_the_past, :discount_cannot_be_be_more_than_total_value + + def expiration_date_cannot_be_in_the_past + errors.add(:expiration_date, "can't be in the past") if !expiration_date.blank? and expiration_date < Date.today + end + + def discount_cannot_be_greater_than_total_value + errors.add(:discount, "can't be greater than total value") unless discount <= total_value + end +end +------------------------------------------------------------------ + == Changelog http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks -- cgit v1.2.3