Class: DataMapper::Validations::ValidationErrors

### Home Classes Methods

### In Files

- lib/dm-validations/validation_errors.rb

### Parent

Object

### Methods

- ::default_error_message
- ::default_error_messages=
- ::new
- #[]
- #add
- #clear!
- #each
- #empty?
- #full_messages
- #method_missing
- #on
- #respond_to?

### Included Modules

- Enumerable

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::InvalidContextError
- DataMapper::Validate
- DataMapper::ValidationError
- DataMapper::Validations
- DataMapper::Validations::AbsenceValidator
- DataMapper::Validations::AcceptanceValidator
- DataMapper::Validations::AutoValidations
- DataMapper::Validations::AutoValidations::ModelExtension
- DataMapper::Validations::ClassMethods
- DataMapper::Validations::ConfirmationValidator
- DataMapper::Validations::Context
- DataMapper::Validations::ContextualValidators
- DataMapper::Validations::Format
- DataMapper::Validations::Format::Email
- DataMapper::Validations::Format::Url
- DataMapper::Validations::FormatValidator
- DataMapper::Validations::GenericValidator
- DataMapper::Validations::LengthValidator
- DataMapper::Validations::MethodValidator
- DataMapper::Validations::NumericalityValidator
- DataMapper::Validations::OrderedHash
- DataMapper::Validations::PresenceValidator
- DataMapper::Validations::PrimitiveTypeValidator
- DataMapper::Validations::UniquenessValidator
- DataMapper::Validations::UnknownValidationFormat
- DataMapper::Validations::ValidatesAbsence
- DataMapper::Validations::ValidatesAcceptance
- DataMapper::Validations::ValidatesConfirmation
- DataMapper::Validations::ValidatesFormat
- DataMapper::Validations::ValidatesLength
- DataMapper::Validations::ValidatesNumericality
- DataMapper::Validations::ValidatesPresence
- DataMapper::Validations::ValidatesPrimitiveType
- DataMapper::Validations::ValidatesUniqueness
- DataMapper::Validations::ValidatesWithBlock
- DataMapper::Validations::ValidatesWithMethod
- DataMapper::Validations::ValidatesWithin
- DataMapper::Validations::ValidationErrors
- DataMapper::Validations::WithinValidator
- Object

No matching classes.

# DataMapper::Validations::ValidationErrors

@author Guy van den Berg @since 0.9

### Attributes

resource[R]

### Public Class Methods

default_error_message(key, field, *values) click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 40
    def self.default_error_message(key, field, *values)
      field = DataMapper::Inflector.humanize(field)
      @@default_error_messages[key] % [field, *values].flatten
    end

default_error_messages=(default_error_messages) click to toggle source

Holds a hash with all the default error messages that can be replaced by your own copy or localizations.

    # File lib/dm-validations/validation_errors.rb, line 36
    def self.default_error_messages=(default_error_messages)
      @@default_error_messages = default_error_messages
    end

new(resource) click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 47
    def initialize(resource)
      @resource = resource
      @errors   = DataMapper::Validations::OrderedHash.new { |h,k| h[k] = [] }
    end

### Public Instance Methods

[](property_name) click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 123
    def [](property_name)
      if (property_errors = errors[property_name.to_sym])
        property_errors
      end
    end

add(field_name, message) click to toggle source

Add a validation error. Use the field_name :general if the errors does not apply to a specific field of the Resource.

@param [Symbol] field_name

    The name of the field that caused the error

@param [String] message

    The message to add

    # File lib/dm-validations/validation_errors.rb, line 65
    def add(field_name, message)
      # see 6abe8fff in extlib, but don't enforce
      # it unless Edge version is installed
      if message.respond_to?(:try_call)
        # DM resource
        message = if (resource.respond_to?(:model) &&
                      resource.model.respond_to?(:properties))
                    message.try_call(
                      resource,
                      resource.model.properties[field_name]
                    )
                  else
                    # pure Ruby object
                    message.try_call(resource)
                  end
      end

      (errors[field_name] ||= []) << message
    end

clear!() click to toggle source

Clear existing validation errors.

    # File lib/dm-validations/validation_errors.rb, line 53
    def clear!
      errors.clear
    end

each() click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 105
    def each
      errors.each_value do |v|
        yield(v) unless DataMapper::Ext.blank?(v)
      end
    end

empty?() click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 111
    def empty?
      @errors.all? { |property_name, errors| errors.empty? }
    end

full_messages() click to toggle source

Collect all errors into a single list.

    # File lib/dm-validations/validation_errors.rb, line 86
    def full_messages
      errors.inject([]) do |list, pair|
        list += pair.last
      end
    end

method_missing(meth, *args, &block) click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 115
    def method_missing(meth, *args, &block)
      errors.send(meth, *args, &block)
    end

on(field_name) click to toggle source

Return validation errors for a particular field_name.

@param [Symbol] field_name

    The name of the field you want an error for.

@return [Array<DataMapper::Validations::Error>]

    Array of validation errors or empty array, if there are no errors
    on given field

    # File lib/dm-validations/validation_errors.rb, line 100
    def on(field_name)
      errors_for_field = errors[field_name]
      DataMapper::Ext.blank?(errors_for_field) ? nil : errors_for_field.uniq
    end

respond_to?(method) click to toggle source

    # File lib/dm-validations/validation_errors.rb, line 119
    def respond_to?(method)
      super || errors.respond_to?(method)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.