module ActiveModel::Validations

def context_for_validation

def context_for_validation
  @context_for_validation ||= ValidationContext.new
end

def errors

person.errors # => #
person.valid? # => false
person = Person.new

end
validates_presence_of :name
attr_accessor :name

include ActiveModel::Validations
class Person

error messages.
Returns the +Errors+ object that holds all information about attribute
def errors
  @errors ||= Errors.new(self)
end

def freeze

def freeze
  errors
  context_for_validation
  super
end

def init_internals

def init_internals
  super
  @errors = nil
  @context_for_validation = nil
end

def initialize_dup(other) # :nodoc:

:nodoc:
Clean the +Errors+ object if instance is duped.
def initialize_dup(other) # :nodoc:
  @errors = nil
  super
end

def invalid?(context = nil)

person.invalid?(:new) # => true
person.invalid? # => false
person = Person.new

end
validates_presence_of :name, on: :new
attr_accessor :name

include ActiveModel::Validations
class Person

against (the context is defined on the validations using :on).
Context can optionally be supplied to define which callbacks to test

person.invalid? # => false
person.name = 'david'
person.invalid? # => true
person.name = ''
person = Person.new

end
validates_presence_of :name
attr_accessor :name

include ActiveModel::Validations
class Person

added, +false+ otherwise.
Performs the opposite of valid?. Returns +true+ if errors were
def invalid?(context = nil)
  !valid?(context)
end

def raise_validation_error # :doc:

:doc:
def raise_validation_error # :doc:
  raise(ValidationError.new(self))
end

def run_validations!

def run_validations!
  _run_validate_callbacks
  errors.empty?
end

def valid?(context = nil)

person.valid?(:new) # => false
person.valid? # => true
person = Person.new

end
validates_presence_of :name, on: :new
attr_accessor :name

include ActiveModel::Validations
class Person

against (the context is defined on the validations using :on).
Context can optionally be supplied to define which callbacks to test

person.valid? # => true
person.name = 'david'
person.valid? # => false
person.name = ''
person = Person.new

end
validates_presence_of :name
attr_accessor :name

include ActiveModel::Validations
class Person

added otherwise +false+.
Runs all the specified validations and returns +true+ if no errors were
def valid?(context = nil)
  current_context = validation_context
  context_for_validation.context = context
  errors.clear
  run_validations!
ensure
  context_for_validation.context = current_context
end

def validate!(context = nil)

some :on option will only run in the specified context.
Validations with no :on option will run no matter the context. Validations with

no errors are found, raises +ValidationError+ otherwise.
Runs all the validations within the specified context. Returns +true+ if
def validate!(context = nil)
  valid?(context) || raise_validation_error
end

def validates_with(*args, &block)

class version of this method for more information.
to the class and available as +options+, please refer to the
If you pass any additional configuration options, they will be passed

as these are applied and tested in the callback.
+validates_with+, should instead be placed on the +validates+ method
:unless), which are available on the class version of
Standard configuration options (:on, :if and

end
end
validates_with MyValidator, MyOtherValidator
def instance_validations

validate :instance_validations, on: :create

include ActiveModel::Validations
class Person

You may also pass it multiple classes, like so:

creating your own validator.
Please consult the class method documentation for more information on

end
end
validates_with MyValidator
def instance_validations

validate :instance_validations

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.class
  args.each do |klass|
    validator = klass.new(options.dup, &block)
    validator.validate(self)
  end
end

def validation_context

person.valid?(:custom) #=> true
person.valid?(:new) #=> false
person.valid? #=> false
person = Person.new

end
validates :name, presence: true, if: -> { validation_context != :custom }
attr_accessor :name

include ActiveModel::Validations
class Person

This is useful when running validations except a certain context (opposite to the +on+ option).

Returns the context when running validations.
def validation_context
  context_for_validation.context
end

def validation_context=(context)

def validation_context=(context)
  context_for_validation.context = context
end