Skip to content

Commit 547e695

Browse files
committed
move the strict validations to an appropriate section and some edits [ci skip]
1 parent 1d802f1 commit 547e695

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

railties/guides/source/active_record_validations_callbacks.textile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,6 @@ end
141141

142142
+invalid?+ is simply the inverse of +valid?+. +invalid?+ triggers your validations, returning true if any errors were found in the object, and false otherwise.
143143

144-
h4. Strict Validations
145-
146-
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.
147-
148-
<ruby>
149-
class Person < ActiveRecord::Base
150-
validates :name, :presence => {:strict => true}
151-
end
152-
153-
>> p = Person.new
154-
>> p.valid?
155-
=> ActiveModel::StrictValidationFailed: can't be blank
156-
</ruby>
157-
158144
h4(#validations_overview-errors). +errors[]+
159145

160146
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
531517
end
532518
</ruby>
533519

520+
h3. Strict Validations
521+
522+
You can also specify validations to be strict and raise +ActiveModel::StrictValidationFailed+ when the object is invalid.
523+
524+
<ruby>
525+
class Person < ActiveRecord::Base
526+
validates :name, :presence => { :strict => true }
527+
end
528+
529+
Person.new.valid? => ActiveModel::StrictValidationFailed: Name can't be blank
530+
</ruby>
531+
534532
h3. Conditional Validation
535533

536534
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.

0 commit comments

Comments
 (0)