aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source
diff options
context:
space:
mode:
authorVijay Dev <vijaydev.cse@gmail.com>2012-02-28 18:58:59 +0530
committerVijay Dev <vijaydev.cse@gmail.com>2012-02-28 18:58:59 +0530
commit547e695551e223e4d92762d3c176187e6780e524 (patch)
tree7a13f69f1d622ca0903d346cb2ac9a4e838c291e /railties/guides/source
parent1d802f19751181d005d0b86ae75f4ba16fc37e47 (diff)
downloadrails-547e695551e223e4d92762d3c176187e6780e524.tar.gz
rails-547e695551e223e4d92762d3c176187e6780e524.tar.bz2
rails-547e695551e223e4d92762d3c176187e6780e524.zip
move the strict validations to an appropriate section and some edits [ci skip]
Diffstat (limited to 'railties/guides/source')
-rw-r--r--railties/guides/source/active_record_validations_callbacks.textile26
1 files changed, 12 insertions, 14 deletions
diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile
index fc9a2ad30c..349d02c1f6 100644
--- a/railties/guides/source/active_record_validations_callbacks.textile
+++ b/railties/guides/source/active_record_validations_callbacks.textile
@@ -141,20 +141,6 @@ end
+invalid?+ is simply the inverse of +valid?+. +invalid?+ triggers your validations, returning true if any errors were found in the object, and false otherwise.
-h4. Strict Validations
-
-Rails can also be specify strict validations. You can use the +:strict+ option to set that validation as strict. If an object fails a strict validation then an +ActiveModel::StrictValidationFailed+ error message is raised.
-
-<ruby>
-class Person < ActiveRecord::Base
- validates :name, :presence => {:strict => true}
-end
-
->> p = Person.new
->> p.valid?
-=> ActiveModel::StrictValidationFailed: can't be blank
-</ruby>
-
h4(#validations_overview-errors). +errors[]+
To verify whether or not a particular attribute of an object is valid, you can use +errors[:attribute]+. It returns an array of all the errors for +:attribute+. If there are no errors on the specified attribute, an empty array is returned.
@@ -531,6 +517,18 @@ class Person < ActiveRecord::Base
end
</ruby>
+h3. Strict Validations
+
+You can also specify validations to be strict and raise +ActiveModel::StrictValidationFailed+ when the object is invalid.
+
+<ruby>
+class Person < ActiveRecord::Base
+ validates :name, :presence => { :strict => true }
+end
+
+Person.new.valid? => ActiveModel::StrictValidationFailed: Name can't be blank
+</ruby>
+
h3. 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 +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.