Class: DataMapper::Validations::PresenceValidator

### Home Classes Methods

### In Files

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

### Parent

DataMapper::Validations::GenericValidator

### Methods

- #boolean_type?
- #call
- #default_error
- #present?

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

@author Guy van den Berg @since 0.9

### Public Instance Methods

call(target) click to toggle source

    # File lib/dm-validations/validators/required_field_validator.rb, line 7
    def call(target)
      value    = target.validation_property_value(field_name)
      property = get_resource_property(target, field_name)
      return true if present?(value, property)

      error_message = @options[:message] || default_error(property)
      add_error(target, error_message, field_name)

      false
    end

### Protected Instance Methods

boolean_type?(property) click to toggle source

Is the property a boolean property?

@return [Boolean]

    Returns true for Boolean, ParanoidBoolean, TrueClass and other
    properties. Returns false for other property types or for
    non-properties.

    # File lib/dm-validations/validators/required_field_validator.rb, line 38
    def boolean_type?(property)
      property ? property.primitive == TrueClass : false
    end

default_error(property) click to toggle source

    # File lib/dm-validations/validators/required_field_validator.rb, line 27
    def default_error(property)
      actual = boolean_type?(property) ? :nil : :blank
      ValidationErrors.default_error_message(actual, field_name)
    end

present?(value, property) click to toggle source

Boolean property types are considered present if non-nil. Other property types are considered present if non-blank. Non-properties are considered present if non-blank.

    # File lib/dm-validations/validators/required_field_validator.rb, line 23
    def present?(value, property)
      boolean_type?(property) ? !value.nil? : !DataMapper::Ext.blank?(value)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.