-
Notifications
You must be signed in to change notification settings - Fork 183
Rich Validations
Scharrels edited this page Aug 5, 2015
·
13 revisions
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, after: :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 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, next_to: :syntax do
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