Class: PhoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/phonie/railties/validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/phonie/railties/validator.rb', line 4

def validate_each(object, attribute, value)
  return if value.blank?
  phone = Phonie::Phone.parse(value)

  if phone.nil?
    object.errors.add(attribute, :invalid_phone_number)
  else
    formated = phone.format( phone.extension ? :default_with_extension : :default)
    object.send("#{attribute}=", formated) if object.respond_to?("#{attribute}=")
  end
end