-
Notifications
You must be signed in to change notification settings - Fork 183
Rich Validations
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.
To chain validations to one prerequisite.
validates :username, :password, presence: true do
validate :password_ok?
end
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
group :syntax, if: -> {} do
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
disable_group :validation
remove_validation :title, :presence
-
: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.