module ActiveModel::Validations::ClassMethods
def validates_with(*args, &block)
end
options[:my_custom_key] # => "my custom value"
def validate(record)
class MyValidator < ActiveModel::Validator
end
validates_with MyValidator, my_custom_key: 'my custom value'
include ActiveModel::Validations
class Person
to the class and available as +options+:
If you pass any additional configuration options, they will be passed
See ActiveModel::Validations#validates! for more information.
* :strict - Specifies whether validation should be strict.
+false+ value.
The method, proc, or string should return or evaluate to a +true+ or
unless: Proc.new { |user| user.signup_step <= 2 }).
(e.g. unless: :skip_validation, or
determine if the validation should not occur
* :unless - Specifies a method, proc, or string to call to
+false+ value.
The method, proc, or string should return or evaluate to a +true+ or
or if: Proc.new { |user| user.signup_step > 2 }).
if the validation should occur (e.g. if: :allow_validation,
* :if - Specifies a method, proc, or string to call to determine
on: [:create, :custom_validation_context])
on: :custom_validation_context or
or an array of symbols. (e.g. on: :create or
Runs in all validation contexts by default +nil+. You can pass a symbol
* :on - Specifies the contexts where this validation is active.
Configuration options:
end
validates_with MyValidator, MyOtherValidator, on: :create
include ActiveModel::Validations
class Person
You may also pass it multiple classes, like so:
end
end
# ...
def some_complex_logic
private
end
end
record.errors.add :base, 'This record is invalid'
if some_complex_logic
def validate(record)
class MyValidator < ActiveModel::Validator
end
validates_with MyValidator
include ActiveModel::Validations
class Person
to add errors based on more complex conditions.
Passes the record off to the class or classes specified and allows them
def validates_with(*args, &block) options = args.extract_options! options[:class] = self args.each do |klass| validator = klass.new(options, &block) if validator.respond_to?(:attributes) && !validator.attributes.empty? validator.attributes.each do |attribute| _validators[attribute.to_sym] << validator end else _validators[nil] << validator end validate(validator, options) end end