Module: DataMapper::Validations::ValidatesWithMethod

### Home Classes Methods

### In Files

- lib/dm-validations/validators/method_validator.rb

### Methods

- #validates_with_method

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

### Public Instance Methods

validates_with_method(*fields) click to toggle source

Validate using method called on validated object. The method must to return either true, or a pair of [false, error message string], and is specified as a symbol passed with :method option.

This validator does support multiple fields being specified at a time, but we encourage you to use it with one property/method at a time.

Real world experience shows that method validation is often useful when attribute needs to be virtual and not a property name.

@example Usage

     require 'dm-validations'

    class Page
      include DataMapper::Resource

      property :zip_code, String

      validates_with_method :zip_code,
                            :method => :in_the_right_location?

      def in_the_right_location?
        if @zip_code == "94301"
          return true
        else
          return [false, "You're in the wrong zip code"]
        end
      end

      # A call to valid? will return false and
      # populate the object's errors with "You're in the
      # wrong zip code" unless zip_code == "94301"
    end

    # File lib/dm-validations/validators/method_validator.rb, line 59
    def validates_with_method(*fields)
      validators.add(MethodValidator, *fields)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.