mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added information about grouping conditional validations
This commit is contained in:
parent
50b2eb8cbd
commit
8c68bcf829
1 changed files with 15 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue