instance method
validates_with
Ruby on Rails 7.0.10
Since v3.0.20Signature
validates_with(*args, &block)
Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.
class Person include ActiveModel::Validations validate :instance_validations def instance_validations validates_with MyValidator end end
Please consult the class method documentation for more information on creating your own validator.
You may also pass it multiple classes, like so:
class Person include ActiveModel::Validations validate :instance_validations, on: :create def instance_validations validates_with MyValidator, MyOtherValidator end end
Standard configuration options (:on, :if and :unless), which are available on the class version of validates_with, should instead be placed on the validates method as these are applied and tested in the callback.
If you pass any additional configuration options, they will be passed to the class and available as options, please refer to the class version of this method for more information.
Parameters
-
argsrest -
blockblock
Source
# File activemodel/lib/active_model/validations/with.rb, line 137
def validates_with(*args, &block)
options = args.extract_options!
options[:class] = self.class
args.each do |klass|
validator = klass.new(options, &block)
validator.validate(self)
end
end
Defined in activemodel/lib/active_model/validations/with.rb line 137
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveModel::Validations