Module: Mongoid::Validatable::Macros

Extended by:
ActiveSupport::Concern
Defined in:
build/mongoid-master/lib/mongoid/validatable/macros.rb

Instance Method Summary collapse

Instance Method Details

#validates_associated(*args) ⇒ Object

Validates whether or not an association is valid or not. Will correctly handle has one and has many associations.

Examples:


class Person
  include Mongoid::Document
  embeds_one :name
  embeds_many :addresses

  validates_associated :name, :addresses
end

Parameters:

  • *args (Object...)

    The arguments to pass to the validator.



22
23
24
# File 'build/mongoid-master/lib/mongoid/validatable/macros.rb', line 22

def validates_associated(*args)
  validates_with(AssociatedValidator, _merge_attributes(args))
end

#validates_format_of(*args) ⇒ Object

Validates the format of a field.

Examples:

class Person
  include Mongoid::Document
  field :title

  validates_format_of :title, with: /\A[a-z0-9 \-_]*\z/i
end

Parameters:

  • *args (Object...)

    The names of the field(s) to validate.



54
55
56
# File 'build/mongoid-master/lib/mongoid/validatable/macros.rb', line 54

def validates_format_of(*args)
  validates_with(FormatValidator, _merge_attributes(args))
end

#validates_length_of(*args) ⇒ Object

Validates the length of a field.

Examples:

class Person
  include Mongoid::Document
  field :title

  validates_length_of :title, minimum: 100
end

Parameters:

  • *args (Object...)

    The names of the field(s) to validate.



69
70
71
# File 'build/mongoid-master/lib/mongoid/validatable/macros.rb', line 69

def validates_length_of(*args)
  validates_with(LengthValidator, _merge_attributes(args))
end

#validates_presence_of(*args) ⇒ Object

Validates whether or not a field is present - meaning nil or empty.

Examples:

class Person
  include Mongoid::Document
  field :title

  validates_presence_of :title
end

Parameters:

  • *args (Object...)

    The names of the field(s) to validate.



84
85
86
# File 'build/mongoid-master/lib/mongoid/validatable/macros.rb', line 84

def validates_presence_of(*args)
  validates_with(PresenceValidator, _merge_attributes(args))
end

#validates_uniqueness_of(*args) ⇒ Object

Validates whether or not a field is unique against the documents in the database.

Examples:


class Person
  include Mongoid::Document
  field :title

  validates_uniqueness_of :title
end

Parameters:

  • *args (Object...)

    The arguments to pass to the validator.



39
40
41
# File 'build/mongoid-master/lib/mongoid/validatable/macros.rb', line 39

def validates_uniqueness_of(*args)
  validates_with(UniquenessValidator, _merge_attributes(args))
end