Module: ActiveModel::Validations

### Home Classes Methods

### In Files

- lib/active_model/validations.rb
- lib/active_model/validations/acceptance.rb
- lib/active_model/validations/callbacks.rb
- lib/active_model/validations/confirmation.rb
- lib/active_model/validations/exclusion.rb
- lib/active_model/validations/format.rb
- lib/active_model/validations/inclusion.rb
- lib/active_model/validations/length.rb
- lib/active_model/validations/numericality.rb
- lib/active_model/validations/presence.rb
- lib/active_model/validations/validates.rb
- lib/active_model/validations/with.rb

### Namespace

- MODULE ActiveModel::Validations::Callbacks
- MODULE ActiveModel::Validations::ClassMethods
- MODULE ActiveModel::Validations::HelperMethods
- CLASS ActiveModel::Validations::AcceptanceValidator
- CLASS ActiveModel::Validations::ConfirmationValidator
- CLASS ActiveModel::Validations::ExclusionValidator
- CLASS ActiveModel::Validations::FormatValidator
- CLASS ActiveModel::Validations::InclusionValidator
- CLASS ActiveModel::Validations::LengthValidator
- CLASS ActiveModel::Validations::NumericalityValidator
- CLASS ActiveModel::Validations::PresenceValidator
- CLASS ActiveModel::Validations::WithValidator

### Methods

- #errors
- #invalid?
- #run_validations!
- #valid?
- #validates_with

### Included Modules

- ActiveSupport::Callbacks
- ActiveModel::Validations::HelperMethods

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::Validations

## Active Model Validations

Provides a full validation framework to your objects.

A minimal implementation could be:

    class Person
      include ActiveModel::Validations

      attr_accessor :first_name, :last_name

      validates_each :first_name, :last_name do |record, attr, value|
        record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
      end
    end

Which provides you with the full standard validation stack that you know from Active Record:

    person = Person.new
    person.valid?                   # => true
    person.invalid?                 # => false

    person.first_name = 'zoolander'
    person.valid?                   # => false
    person.invalid?                 # => true
    person.errors                   # => #<OrderedHash {:first_name=>["starts with z."]}>

Note that `ActiveModel::Validations` automatically adds an `errors` method to your instances initialized with a new `ActiveModel::Errors` object, so there is no need for you to do this manually.

## Active Model Acceptance Validator

## Active Model Confirmation Validator

## Active Model Exclusion Validator

## Active Model Format Validator

## Active Model Inclusion Validator

## Active Model Length Validator

## Active Model Numericality Validator

## Active Model Presence Validator

## Active Model validates method

### Public Instance Methods

errors() click to toggle source

Returns the `Errors` object that holds all information about attribute error messages.

    # File lib/active_model/validations.rb, line 185
    def errors
      @errors ||= Errors.new(self)
    end

invalid?(context = nil) click to toggle source

Performs the opposite of `valid?`. Returns true if errors were added, false otherwise.

    # File lib/active_model/validations.rb, line 202
    def invalid?(context = nil)
      !valid?(context)
    end

valid?(context = nil) click to toggle source

Runs all the specified validations and returns true if no errors were added otherwise false. Context can optionally be supplied to define which callbacks to test against (the context is defined on the validations using :on).

    # File lib/active_model/validations.rb, line 192
    def valid?(context = nil)
      current_context, self.validation_context = validation_context, context
      errors.clear
      run_validations!
    ensure
      self.validation_context = current_context
    end

validates_with(*args, &block) click to toggle source

Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions.

    class Person
      include ActiveModel::Validations

      validate :instance_validations

      def instance_validations
        validates_with MyValidator
      end
    end

Please consult the class method documentation for more information on creating your own validator.

You may also pass it multiple classes, like so:

    class Person
      include ActiveModel::Validations

      validate :instance_validations, :on => :create

      def instance_validations
        validates_with MyValidator, MyOtherValidator
      end
    end

Standard configuration options (:on, :if and :unless), which are available on the class version of validates_with, should instead be placed on the `validates` method as these are applied and tested in the callback.

If you pass any additional configuration options, they will be passed to the class and available as `options`, please refer to the class version of this method for more information.

    # File lib/active_model/validations/with.rb, line 134
    def validates_with(*args, &block)
      options = args.extract_options!
      args.each do |klass|
        validator = klass.new(options, &block)
        validator.validate(self)
      end
    end

### Protected Instance Methods

run_validations!() click to toggle source

    # File lib/active_model/validations.rb, line 227
    def run_validations!
      run_callbacks :validate
      errors.empty?
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.