Sha256: 763e9052e52dc0fbce26d80682ecd16add7d03cb4b03cec4fd5edc47daaa45cd

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

# Validator class for phone validations
#
# ==== Examples
#
# Validates that attribute is a valid phone number.
# If empty value passed for attribute it fails.
#
#   class Phone < ActiveRecord::Base
#     attr_accessible :number
#     validates :number, phone: true
#   end
#
# Validates that attribute is a possible phone number.
# If empty value passed for attribute it fails.
#
#   class Phone < ActiveRecord::Base
#     attr_accessible :number
#     validates :number, phone: { possible: true }
#   end
#
# Validates that attribute is a valid phone number.
# Empty value is allowed to be passed.
#
#   class Phone < ActiveRecord::Base
#     attr_accessible :number
#     validates :number, phone: { allow_blank: true }
#   end 
class PhoneValidator < ActiveModel::EachValidator
  # Include all core methods
  include Phonelib::Core

  # Validation method
  def validate_each(record, attribute, value)
    phone = parse(value)
    valid = options[:possible] ? phone.possible? : phone.valid?
    valid = true if options[:allow_blank] && phone.original.blank?

    record.errors.add(attribute, options[:message] || :invalid) unless valid
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phonelib-0.2.4 lib/validators/phone_validator.rb
phonelib-0.2.3 lib/validators/phone_validator.rb
phonelib-0.2.2 lib/validators/phone_validator.rb
phonelib-0.2.1 lib/validators/phone_validator.rb
phonelib-0.2.0 lib/validators/phone_validator.rb