Class: ActiveModel::EachValidator

### Home Classes Methods

### In Files

- lib/active_model/validator.rb

### Parent

ActiveModel::Validator

### Methods

- ::new
- #check_validity!
- #validate
- #validate_each

### 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::EachValidator

`EachValidator` is a validator which iterates through the attributes given in the options hash invoking the `validate_each` method passing in the record, attribute and value.

All Active Model validations are built on top of this validator.

### Attributes

attributes[R]

### Public Class Methods

new(options) click to toggle source

Returns a new validator instance. All options will be available via the `options` reader, however the `:attributes` option will be removed and instead be made available through the `attributes` reader.

    # File lib/active_model/validator.rb, line 139
    def initialize(options)
      @attributes = Array.wrap(options.delete(:attributes))
      raise ":attributes cannot be blank" if @attributes.empty?
      super
      check_validity!
    end

### Public Instance Methods

check_validity!() click to toggle source

Hook method that gets called by the initializer allowing verification that the arguments supplied are valid. You could for example raise an `ArgumentError` when invalid options are supplied.

    # File lib/active_model/validator.rb, line 166
    def check_validity!
    end

validate(record) click to toggle source

Performs validation on the supplied record. By default this will call `validates_each` to determine validity therefore subclasses should override `validates_each` with validation logic.

    # File lib/active_model/validator.rb, line 149
    def validate(record)
      attributes.each do |attribute|
        value = record.read_attribute_for_validation(attribute)
        next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
        validate_each(record, attribute, value)
      end
    end

validate_each(record, attribute, value) click to toggle source

Override this method in subclasses with the validation logic, adding errors to the records `errors` array where necessary.

    # File lib/active_model/validator.rb, line 159
    def validate_each(record, attribute, value)
      raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method"
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.