Class: PhoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/phone_validator.rb

Constant Summary collapse

REGEXP =
/\A
  (\(?\+\d+\)?)?    # optional country code
  [0-9 \-.()]{4,20}
\z/x.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



7
8
9
# File 'lib/validators/phone_validator.rb', line 7

def validate_each record, attribute, value
  record.errors.add attribute, (options[:message] || I18n.t('rails_validations.invalid_phone')) unless value =~ REGEXP
end