Module: DataMapper::Validations::ValidatesWithBlock

### Home Classes Methods

### In Files

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

### Methods

- #validates_with_block

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

@author teamon @since 0.9

### Public Instance Methods

validates_with_block(*fields, &block) click to toggle source

Validate using the given block. The block given needs to return:

result::<Boolean>, Error Message::<String>

  @example [Usage]

      require 'dm-validations'

      class Page
        include DataMapper::Resource

        property :zip_code, String

        validates_with_block do
          if @zip_code == "94301"
            true
          else
            [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"

        # You can also specify field:

        validates_with_block :zip_code do
          if @zip_code == "94301"
            true
          else
            [false, "You're in the wrong zip code"]
          end
        end

        # it will add returned error message to :zip_code field

    # File lib/dm-validations/validators/block_validator.rb, line 41
    def validates_with_block(*fields, &block)
      @__validates_with_block_count ||= 0
      @__validates_with_block_count += 1

      # create method and pass it to MethodValidator
      unless block_given?
        raise ArgumentError, 'You need to pass a block to validates_with_block method'
      end

      method_name = "__validates_with_block_#{@__validates_with_block_count}".to_sym
      define_method(method_name, &block)

      options = fields.last.is_a?(Hash) ? fields.last.pop.dup : {}
      options[:method] = method_name
      fields = [method_name] if fields.empty?

      validators.add(MethodValidator, *fields + [options])
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.