Module: ActiveModel::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Included in:
ActiveRecord::Validations, ActiveResource::Validations
Defined in:
activemodel/lib/active_model/validations.rb,
activemodel/lib/active_model/validations/with.rb,
activemodel/lib/active_model/validations/format.rb,
activemodel/lib/active_model/validations/length.rb,
activemodel/lib/active_model/validations/presence.rb,
activemodel/lib/active_model/validations/callbacks.rb,
activemodel/lib/active_model/validations/validates.rb,
activemodel/lib/active_model/validations/inclusion.rb,
activemodel/lib/active_model/validations/exclusion.rb,
activemodel/lib/active_model/validations/acceptance.rb,
activemodel/lib/active_model/validations/confirmation.rb,
activemodel/lib/active_model/validations/numericality.rb

Overview

Active Model Numericality Validator

Defined Under Namespace

Modules: Callbacks, ClassMethods, HelperMethods Classes: AcceptanceValidator, ConfirmationValidator, ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, NumericalityValidator, PresenceValidator, WithValidator

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Methods included from ActiveSupport::Callbacks

#run_callbacks

Instance Method Details

#dupObject

:nodoc:



176
177
178
179
180
# File 'activemodel/lib/active_model/validations.rb', line 176

def dup # :nodoc:
  copy = super
  copy.initialize_dup(self)
  copy
end

#errorsObject

Returns the Errors object that holds all information about attribute error messages.



184
185
186
# File 'activemodel/lib/active_model/validations.rb', line 184

def errors
  @errors ||= Errors.new(self)
end

#initialize_dup(other) ⇒ Object

Clean the Errors object if instance is duped



169
170
171
172
# File 'activemodel/lib/active_model/validations.rb', line 169

def initialize_dup(other) # :nodoc:
  @errors = nil
  super if defined?(super)
end

#invalid?(context = nil) ⇒ Boolean

Performs the opposite of valid?. Returns true if errors were added, false otherwise.

Returns:

  • (Boolean)


201
202
203
# File 'activemodel/lib/active_model/validations.rb', line 201

def invalid?(context = nil)
  !valid?(context)
end

#valid?(context = nil) ⇒ Boolean

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).

Returns:

  • (Boolean)


191
192
193
194
195
196
197
# File 'activemodel/lib/active_model/validations.rb', line 191

def valid?(context = nil)
  current_context, self.validation_context = validation_context, context
  errors.clear
  run_validations!
ensure
  self.validation_context = current_context
end

#validates_with(*args, &block) ⇒ Object

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.



134
135
136
137
138
139
140
# 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