| send | -> | read_attribute_for_validation |
Hook method defining how an attribute value should be retrieved. By default
this is assumed to be an instance named after the attribute. Override this
method in subclasses should you need to retrieve the value for a given
attribute differently:
class MyClass
include ActiveModel::Validations
def initialize(data = {})
@data = data
end
def read_attribute_for_validation(key)
@data[key]
end
end
|
||
Runs all the specified validations and returns true if no errors were added otherwise false. Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :on).
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
validates :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
validates :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