Module: ActiveModel::Validations

Defined in:
activemodel/lib/active_model/validations.rb,
activemodel/lib/active_model/validations/length.rb,
activemodel/lib/active_model/validations/format.rb,
activemodel/lib/active_model/validations/presence.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/uniqueness.rb,
activemodel/lib/active_model/validations/associated.rb,
activemodel/lib/active_model/validations/numericality.rb,
activemodel/lib/active_model/validations/confirmation.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

:nodoc:



5
6
7
8
9
# File 'activemodel/lib/active_model/validations.rb', line 5

def self.included(base) # :nodoc:
  base.extend(ClassMethods)
  base.__send__(:include, ActiveSupport::Callbacks)
  base.define_callbacks :validate, :validate_on_create, :validate_on_update
end

Instance Method Details

- (Object) errors

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



85
86
87
# File 'activemodel/lib/active_model/validations.rb', line 85

def errors
  @errors ||= Errors.new
end

- (Boolean) valid?

Runs all the specified validations and returns true if no errors were added otherwise false.

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'activemodel/lib/active_model/validations.rb', line 90

def valid?
  errors.clear

  run_callbacks(:validate)
  
  if responds_to?(:validate)
    ActiveSupport::Deprecations.warn "Base#validate has been deprecated, please use Base.validate :method instead"
    validate
  end

  if new_record?
    run_callbacks(:validate_on_create)

    if responds_to?(:validate_on_create)
      ActiveSupport::Deprecations.warn(
        "Base#validate_on_create has been deprecated, please use Base.validate_on_create :method instead")
      validate_on_create
    end
  else
    run_callbacks(:validate_on_update)

    if responds_to?(:validate_on_update)
      ActiveSupport::Deprecations.warn(
        "Base#validate_on_update has been deprecated, please use Base.validate_on_update :method instead")
      validate_on_update
    end
  end

  errors.empty?
end