aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_record_validations_callbacks.textile
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/source/active_record_validations_callbacks.textile')
-rw-r--r--railties/guides/source/active_record_validations_callbacks.textile22
1 files changed, 19 insertions, 3 deletions
diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile
index 514d0322b9..19bd4ad0f1 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.
@@ -822,7 +837,7 @@ The selectors to customize the style of error messages are:
* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
* +#errorExplanation ul li+ - Style for the list items with individual error messages.
-Scaffolding for example generates +public/stylesheets/scaffold.css+, which defines the red-based style you saw above.
+Scaffolding for example generates +app/assets/stylesheets/scaffold.css.scss+, which later compiles to +app/assets/stylesheets/scaffold.css+ and defines the red-based style you saw above.
The name of the class and the id can be changed with the +:class+ and +:id+ options, accepted by both helpers.
@@ -882,8 +897,9 @@ The macro-style class methods can also receive a block. Consider using this styl
class User < ActiveRecord::Base
validates_presence_of :login, :email
- before_create {|user| user.name = user.login.capitalize
- if user.name.blank?}
+ before_create do |user|
+ user.name = user.login.capitalize if user.name.blank?
+ end
end
</ruby>