Skip to content
Jorne Kandziora edited this page Feb 1, 2016 · 13 revisions

NOTE: This document was used during the implementation of validation groups. See http://trailblazer.to/gems/reform/validation.html#validation-group for the current documentation of this feature.

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, if: :syntax do # runs only if syntax is correct
  validate :password_ok?
end

Or nest it directly

group :syntax do
  validates :email, :password, presence: true
  validates :email, email: true # here, another validation that is not presence.

  group :authentication, if: :syntax do # runs only if syntax is correct
    validate :password_ok?
  end
end

Conditions

group :syntax, if: -> {} do

Grouping and inheritance

Redefining a group in an inherited form overwrites the group:

RememberMeSignIn < SignIn
  group :syntax do
    validates :email, :password, presence: true
    validates :email, email: true # here, another validation that is not presence.
    validates :remember_me, :inclusion => {:in => [true, false]}
  end
end

Alternatively, you can define a different group:

RememberMeSignIn < SignIn
  group :more_syntax, after: :syntax do # will add group after :syntax, same level.
    validates :remember_me, :inclusion => {:in => [true, false]}
  end
end

or extend the existing group with some extra validations

RememberMeSignIn < SignIn
  group :syntax, inherit: true do # will extend :syntax.
    validates :remember_me, :inclusion => {:in => [true, false]}
  end
end

Disabling groups

disable_group :validation

Disabling validations

remove_validation :title, :presence

Options

  • :if with a symbol, the specified group must have been run successfully in order to evaluate the group.
  • :after will insert the group/validation after the specified one, on the same level.