Class: PhoneNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/phone_number_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
# File 'app/models/phone_number_validator.rb', line 2

def validate_each(record, attribute, value)
  if value.blank?
    record.errors[attribute] << 'is required' unless options[:allow_blank] || options[:allow_nil]
    return
  end

  record.errors[attribute] << (options[:message] || 'is not a valid phone number') if value.gsub(/[^\d]/, '').length < 10
end