Sha256: 6031514024b1c15639c0b6731a853273b2c30e1ed71e247824b0d3f6270e97b2

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

module Phonelib
  # Module includes all ActiveModel validators classes
  module Validators
    # 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?

        # TODO: change to be from translations
        error = "is not valid"
        record.errors.add(attribute, (options[:message] || error)) unless valid
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
phonelib-0.1.3 lib/phonelib/validators.rb
phonelib-0.1.2 lib/phonelib/validators.rb
phonelib-0.1.1 lib/phonelib/validators.rb
phonelib-0.1.0 lib/phonelib/validators.rb
phonelib-0.0.2 lib/phonelib/validators.rb
phonelib-0.0.1 lib/phonelib/validators.rb