Module: Mongoid::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Components
Defined in:
lib/mongoid/validations.rb,
lib/mongoid/validations/associated.rb,
lib/mongoid/validations/uniqueness.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods Classes: AssociatedValidator, UniquenessValidator

Instance Method Summary collapse

Instance Method Details

#begin_validateObject

Begin the associated validation.

Examples:

Begin validation.

document.begin_validate

Since:

  • 2.1.9



19
20
21
# File 'lib/mongoid/validations.rb', line 19

def begin_validate
  Threaded.begin_validate(self)
end

#exit_validateObject

Exit the associated validation.

Examples:

Exit validation.

document.exit_validate

Since:

  • 2.1.9



29
30
31
# File 'lib/mongoid/validations.rb', line 29

def exit_validate
  Threaded.exit_validate(self)
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



45
46
47
48
49
50
51
52
53
54
# File 'lib/mongoid/validations.rb', line 45

def read_attribute_for_validation(attr)
  if relations[attr.to_s]
    begin_validate
    relation = send(attr)
    exit_validate
    relation.do_or_do_not(:in_memory) || relation
  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



69
70
71
# File 'lib/mongoid/validations.rb', line 69

def valid?(context = nil)
  super context ? context : (new? ? :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



81
82
83
# File 'lib/mongoid/validations.rb', line 81

def validated?
  Threaded.validated?(self)
end