lib/validators/phone_validator.rb in phonelib-0.6.13 vs lib/validators/phone_validator.rb in phonelib-0.6.14

- old
+ new

@@ -34,23 +34,37 @@ # attr_accessible :number, :mobile # validates :number, phone: { types: [:mobile, :fixed], allow_blank: true } # validates :mobile, phone: { possible: true, types: :mobile } # end # +# Validates that attribute does not include an extension. +# The default setting is to allow extensions +# +# class Phone < ActiveRecord::Base +# attr_accessible :number +# validates :number, phone: { extensions: false } +# end +# class PhoneValidator < ActiveModel::EachValidator # Include all core methods include Phonelib::Core # Validation method def validate_each(record, attribute, value) return if options[:allow_blank] && value.blank? + options[:extensions] ||= true phone = parse(value, specified_country(record)) valid = if simple_validation? phone.send(validate_method) else (phone_types(phone) & types).size > 0 end + + # We default to not-allowing extensions for fax numbers + if valid && options[:extensions] === false && !phone.extension.empty? + valid = false + end record.errors.add(attribute, message, options) unless valid end private