Skip to content
Scharrels edited this page Aug 5, 2015 · 13 revisions

Simple chaining/cascading

To chain validations to one prerequisite.

validates :username, :password, presence: true do
  validate :password_ok?
end

Grouping

Allows more prerequisite validations and unlimited nesting.

group :syntax do
  validates :email, :password, presence: true
  validates :email, email: true # here, another validation that is not presence.
end
     
group :authentication, after: :syntax do # runs only if syntax is correct
  validate :password_ok?
end

Conditions

group :syntax, if: -> {} do

Grouping and inheritance

RememberMeSignIn < SignIn
  group :more_syntax, next_to: :syntax do
    validates :remember_me, :inclusion => {:in => [true, false]}
  end
end

Disabling groups

disable_group :validation