validations
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 its specific options.
validates_acceptance_of
Validate the acceptance of a boolean field:
Options:
- :message Supply a custom error message.
- :accept Specify the accepted value. Default: "1"
validates_acceptance_of :terms
validates_associated
Validate associated documents along with the parent:
Options:
- :message Supply a custom error message.
validates_associated :addresses, :employers
validates_confirmation_of
Validate confirmation of a field via an accessor with "_confirmation" as its suffix:
Options:
- :message Supply a custom error message.
validates_confirmation_of :password
validates_exclusion_of
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"]
validates_format_of
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]/
validates_inclusion_of
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"]
validates_length_of
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
validates_numericality_of
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
validates_presence_of
Validate that a field exists:
Options:
- :message Supply a custom error message.
validates_presence_of :first_name
validates_uniqueness_of
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.
- :case_sensitive Whether to use case sensitive matching. Defaults to true.
- :scope Scope uniqueness checks to the value of this field.
validates_uniqueness_of :ssn