diff options
author | CassioMarques <cassiommc@gmail.com> | 2008-11-12 21:40:29 -0200 |
---|---|---|
committer | CassioMarques <cassiommc@gmail.com> | 2008-11-12 21:41:35 -0200 |
commit | fdd1ef5ca26de29eee3e5246e609c883ea3d9319 (patch) | |
tree | 87ea538f2dd1c58d66a854853d7b89827d16c5aa /railties/doc/guides/source | |
parent | af320ef2202feaca376f759968aa4f5d977b0db3 (diff) | |
download | rails-fdd1ef5ca26de29eee3e5246e609c883ea3d9319.tar.gz rails-fdd1ef5ca26de29eee3e5246e609c883ea3d9319.tar.bz2 rails-fdd1ef5ca26de29eee3e5246e609c883ea3d9319.zip |
Added 'custom validations' to AR validations and callbacks guide
Diffstat (limited to 'railties/doc/guides/source')
-rw-r--r-- | railties/doc/guides/source/activerecord_validations_callbacks.txt | 30 |
1 files changed, 30 insertions, 0 deletions
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 |