instance method validates_with

Ruby on Rails 3.2.22.5

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

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

args rest
block block
Source
# File activemodel/lib/active_model/validations/with.rb, line 134
    def validates_with(*args, &block)
      options = args.extract_options!
      args.each do |klass|
        validator = klass.new(options, &block)
        validator.validate(self)
      end
    end

Defined in activemodel/lib/active_model/validations/with.rb line 134 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveModel::Validations

Type at least 2 characters to search.

↑↓ navigate · open · esc close