diff options
author | Michal Papis <mpapis@niczsoft.com> | 2011-04-16 22:56:58 +0200 |
---|---|---|
committer | Michal Papis <mpapis@niczsoft.com> | 2011-04-16 22:56:58 +0200 |
commit | 8c68bcf829ead305e2459c2b90850b43464ec7d7 (patch) | |
tree | 9c201e00a1c10c12b8447df9ce711d4cd051b608 /railties | |
parent | 50b2eb8cbd6cb923026b8fbb98482e06cdbd21ce (diff) | |
download | rails-8c68bcf829ead305e2459c2b90850b43464ec7d7.tar.gz rails-8c68bcf829ead305e2459c2b90850b43464ec7d7.tar.bz2 rails-8c68bcf829ead305e2459c2b90850b43464ec7d7.zip |
Added information about grouping conditional validations
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/active_record_validations_callbacks.textile | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile index 514d0322b9..8882a5705b 100644 --- a/railties/guides/source/active_record_validations_callbacks.textile +++ b/railties/guides/source/active_record_validations_callbacks.textile @@ -552,6 +552,21 @@ class Account < ActiveRecord::Base end </ruby> +h4. Grouping conditional validations + +Sometimes it is useful to have multiple validations use one condition, it can be easily achieved using +with_options+. + +<ruby> +class User < ActiveRecord::Base + with_options :if => :is_admin? do |admin| + admin.validates_length_of :password, :minimum => 10 + admin.validates_presence_of :email + end +end +</ruby> + +All validations inside of +with_options+ block will have automatically passed the condition +:if => :is_admin?+ + h3. Creating Custom Validation Methods When the built-in validation helpers are not enough for your needs, you can write your own validation methods. |