Validation
Mongoid includes ActiveModel::Validation to supply the basic validation plus an additional associated and uniqueness validator.
See ActiveModel::Validations documentation for more information.
Common options that can be passed to all validations:
- :allow_nil Specify whether to validate on a nil attribute.
- :if Only run if the supplied value evaluates to true.
- :on Only run when specified, supports :create and :update.
- :unless Only run if the supplied value evaluates to false.
In addition to those validations, information is provided with each macro about it's specific options.
Validate the acceptance of a boolean field:
Options:- :message Supply a custom error message.
- :accept Specify the accepted value.
validates_acceptance_of :terms
Validate associated documents along with the parent:
Options:- :message Supply a custom error message.
validates_associated :addresses, :employers
Validate confirmation of a field via an accessor with "_confirmation" as its suffix:
Options:- :message Supply a custom error message.
validates_confirmation_of :password
Validate items are excluded from a collection:
Options:- :in A list or range the item must not be included in.
- :message Supply a custom error message.
validates_exclusion_of :employers, :in => ["Hashrocket"]
Validate the format of a field:
Options:- :in A list or range the item must not be included in.
- :allow_blank Specify whether to validate blank attributes.
- :with The regular expression to match against.
- :without The regular expression to check it does not match.
- :message Supply a custom error message.
validates_format_of :title, :with => /[A-Za-z]/
Validate the inclusion of an item in a collection:
Options:- :allow_blank Specify whether to validate blank attributes.
- :in The list or range the check the value against.
- :message Supply a custom error message.
validates_inclusion_of :employers, :in => ["Hashrocket"]
Validate the length of a field:
Options:- :allow_blank Specify whether to validate blank attributes.
- :in Specify the range the length of the attribute can fall within.
- :maximum Specify the maximum length of the attribute.
- :message Supply a custom error message.
- :minimum Specify the minimum length of the attribute.
- :tokenizer A block to define how the string should be broken up.
- :too_long Define a custom message if the attribute is too long.
- :too_short Define a custom message if the attribute is too short.
- :within Specify the range the length of the attribute can fall within.
- :wrong_length Define a custom message for an incorrect length.
validates_length_of :password, :minimum => 8, :maximum => 16
Validate the numericality of a field:
Options:- :equal_to Specify a value the field must be exactly.
- :even Set that the value must be even.
- :greater_than Specify a value the field must be greater than.
- :greater_than_or_equal_to Specify a value the field must be greater than or equal to.
- :less_than Specify a value the field must be less than.
- :less_than_or_equal_to Specify a value the field must be less than or equal to.
- :message Supply a custom error message.
- :odd Set that the value must be odd.
- :only_integer Set whether the value has to be an integer.
validates_numericality_of :age, :even => true
Validate that a field exists:
Options:- :message Supply a custom error message.
validates_presence_of :first_name
Validate that the field is unique in the database:
Note that for embedded documents, this will only check that the field is unique within the context of the parent document, not the entire database.
Options:- :message Supply a custom error message.
validates_uniqueness_of :ssn