Module: Mongoid::Validatable

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Composable
Defined in:
lib/mongoid/validatable.rb,
lib/mongoid/validatable/format.rb,
lib/mongoid/validatable/length.rb,
lib/mongoid/validatable/macros.rb,
lib/mongoid/validatable/presence.rb,
lib/mongoid/validatable/queryable.rb,
lib/mongoid/validatable/associated.rb,
lib/mongoid/validatable/uniqueness.rb,
lib/mongoid/validatable/localizable.rb

Overview

This module provides additional validations that ActiveModel does not provide: validates_associated and validates_uniqueness_of.

Defined Under Namespace

Modules: ClassMethods, Localizable, Macros, Queryable Classes: AssociatedValidator, FormatValidator, LengthValidator, PresenceValidator, UniquenessValidator

Instance Method Summary collapse

Instance Method Details

#begin_validateObject

Begin the associated validation.

Examples:

Begin validation.

document.begin_validate

Since:

  • 2.1.9



30
31
32
# File 'lib/mongoid/validatable.rb', line 30

def begin_validate
  Threaded.begin_validate(self)
end

#exit_validateObject

Exit the associated validation.

Examples:

Exit validation.

document.exit_validate

Since:

  • 2.1.9



40
41
42
# File 'lib/mongoid/validatable.rb', line 40

def exit_validate
  Threaded.exit_validate(self)
end

#performing_validations?(options = {}) ⇒ true, false

Given the provided options, are we performing validations?

Examples:

Are we performing validations?

document.performing_validations?(validate: true)

Parameters:

  • options (Hash) (defaults to: {})

    The options to check.

Returns:

  • (true, false)

    If we are validating.

Since:

  • 4.0.0



54
55
56
# File 'lib/mongoid/validatable.rb', line 54

def performing_validations?(options = {})
  options[:validate].nil? ? true : options[:validate]
end

#read_attribute_for_validation(attr) ⇒ Object

Overrides the default ActiveModel behaviour since we need to handle validations of relations slightly different than just calling the getter.

Examples:

Read the value.

person.read_attribute_for_validation(:addresses)

Parameters:

  • attr (Symbol)

    The name of the field or relation.

Returns:

  • (Object)

    The value of the field or the relation.

Since:

  • 2.0.0.rc.1



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongoid/validatable.rb', line 70

def read_attribute_for_validation(attr)
  attribute = attr.to_s
  if relations.has_key?(attribute)
    begin_validate
    relation = without_autobuild { send(attr) }
    exit_validate
    relation.do_or_do_not(:in_memory) || relation
  elsif fields[attribute].try(:localized?)
    attributes[attribute]
  else
    send(attr)
  end
end

#valid?(context = nil) ⇒ true, false

Determine if the document is valid.

Examples:

Is the document valid?

person.valid?

Is the document valid in a context?

person.valid?(:create)

Parameters:

  • context (Symbol) (defaults to: nil)

    The optional validation context.

Returns:

  • (true, false)

    True if valid, false if not.

Since:

  • 2.0.0.rc.6



97
98
99
# File 'lib/mongoid/validatable.rb', line 97

def valid?(context = nil)
  super context ? context : (new_record? ? :create : :update)
end

#validated?true, false

Used to prevent infinite loops in associated validations.

Examples:

Is the document validated?

document.validated?

Returns:

  • (true, false)

    Has the document already been validated?

Since:

  • 2.0.0.rc.2



109
110
111
# File 'lib/mongoid/validatable.rb', line 109

def validated?
  Threaded.validated?(self)
end

#validating_with_query?true, false

Are we currently performing a validation that has a query?

Examples:

Are we validating with a query?

document.validating_with_query?

Returns:

  • (true, false)

    If we are validating with a query.

Since:

  • 3.0.2



121
122
123
# File 'lib/mongoid/validatable.rb', line 121

def validating_with_query?
  self.class.validating_with_query?
end